I m new in using interface in c#.so if any one would help to give details overview of interface with practicle example..? i would be highly thankfull to that person.
jatric
I m new in using interface in c#.so if any one would help to give details overview of interface with practicle example..? i would be highly thankfull to that person.
jatric
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niradhip ChakrabortyPosted Jan 18, 2008, 4:22 PM
What Interfaces Are
Interfaces basically define a blueprint for a class or a struct. The programmed definition of an interface looks very similar to a class, but nothing is implemented. Interfaces define the properties, methods, events, and indexers, but the interface does not define the implementation of any of these. It just declares their existence. Interfaces will not actually define any functionality. They just define ways in which interactions with a class takes place.
What Interfaces Are Not
Interfaces should not be confused with inheritance. They are two very different things. Inheritance will define a lot of the implementation and is used for code reuse. Interfaces are merely a definition for how communication with the implementing classes must take place. It is like a written contract. A class "signing" the contract will agree to perform certain specified actions in any way it wishes, but it must perform the specified actions.
When to Use Interfaces
Interfaces allow us to create nice layouts for what a class is going to implement. Because of the guarantee the interface gives us, when many components use the same interface it allows us to easily interchange one component for another which is using the same interface. Dynamic programs begin to form easily from this.
To define an interface in C# you can use the interface keyword. The following code snippet demonstrate how to define and implement an interface.
// Define the IFoo interface.
public interface IFoo
{
void DoSomething();
}
// Implement IFoo in Foo class.
public class Foo : IFoo
{
public void DoSomething()
{
}
}
jatin sharmaPosted Jan 15, 2008, 4:34 AM
{ public class Animal { protected string Characteristis; public string characteristics { get { return this.Characteristis; } set { this.Characteristis=value; } } } interface IIntelligence { /// Interface method declaration bool intelligent_behavior(); } class HumanBeing: Animal, IIntelligence { public HumanBeing() { characteristics = "HumanBeing are Animal(mammals)"; } /// Interface method definition in the class that implements it public bool intelligent_behavior() { Console.WriteLine("{0} and have intelligence",characteristics); return true } } class Whales: Animal { public Whales() { characteristics = "Whales are Animal(mammals)"; Console.WriteLine("{0}",characteristics); } } class InterfaceApp { public static void Main(string[] args) { Whales whale = new Whales(); HumanBeing human = new HumanBeing(); /// The human object is casted to the interface type IIntelligence humanIQ = (IIntelligence)human; humanIQ.intelligent_behavior(); Console.Read(); } } }