Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why Interfaces use ?
Can u give an Implementation ?
Posted

Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference.
Refer more: Interfaces in C# (For Beginners)[^]

Interfaces describe a group of related functionalities that can belong to any class or struct. You define an interface by using the interface keyword.
Refer: http://msdn.microsoft.com/en-us/library/ms173156.aspx[^]

What You Need to Know to Move from C++ to C#[^]
When To Use Interfaces[^]
Interfaces in C#[^]

Implementing an interface declared in C# from C++[^]

Following thread describes:
  • Understand the Purpose of Interfaces.
  • Define an Interface.
  • Use an Interface.
  • Implement Interface Inheritance.

Have a look: Interfaces[^]
 
Share this answer
 
Comments
Manas Bhardwaj 16-Jul-12 5:56am    
well explained +5!
Prasad_Kulkarni 16-Jul-12 8:29am    
Thank you Manas!
In C# you can only inherit from a single base class. C++ is different, so I won't talk about that, so as not to confuse.

So you have a class which needs to derive from UserControl. Fine, you inherit UserControl in your class definition and off you go - you have a control you can add other controls to, and display.
C#
class MyClass : UserControl
   {
   }
What if you also want this control to be used in a foreach loop, because it is a list of items in a database? For that, you need to implement a few methods or foreach won't work. But how do you tell the compiler that your class implements the required methods? Simple, you add the IEnumerable interface to your class definition:
C#
class MyClass : UserControl, IEnumerable<string>
   {
   }
Now, in order for your code to compile, you have to add definitions of all the required methods:
XML
public IEnumerator<string> GetEnumerator() { ... }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { ... }
When you do you have completed your side of the contract, and you can use your class in a foreach:
private void xxx()
    {
    MyClass mine = new MyClass();
    foreach (string s in mine)
        {
        }
    }
And, if you code them as you are supposed to (which is pretty easy most of the time) it will all work.

That is what an Interface is: a contract between you and the interface user. If you follow the rules, you can be a member of the club, and get whatever benefits the club provides.
 
Share this answer
 
Comments
Manas Bhardwaj 16-Jul-12 5:56am    
Nice +5!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900