Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi friends...

How to use interfaces in c#.net?

Can any one explain the concept of interface in detail?

Thanks in advance
Posted
Updated 13-Jun-15 9:47am
v2
Comments
CHill60 13-Jun-15 7:40am    
There is a pattern building up in the questions that you are posting.
Please read the guidelines for the forum carefully before posting your next question. This is not a tutorial section, you are expected to do your own research.
LLLLGGGG 13-Jun-15 18:19pm    
Interfaces are basically abstract classes: they contain only the "structure" of a specific kind of classes. For example: if you use wpf, you will surely have encountered IValueConverter. This is an interface you MUST implement in your class in order to make wpf use that class to convert values because it needs specific methods. Think an interface as a base class, but you cannot access the base code (because it actually does not exist) and you MUST write your own version of each method.

The advantages of the interfaces come in when you are instantiating a class of a generic type, but with a specific use. For example let's suppose you have the interface IShape. Classes Hexagon, Square and circle implement it. Now you need to write a method which outputs the area of a shape. The IShape interface contains the method GetArea and, as the above cited classes implement IShape, the developer has written a GetArea method for each one. Now what you have to do is simply write a method and ask a reference to an IShape in its parameters and then call IShape.GetArea and, based on what you pass inside that method (which could be either an Hexagon, Square or Circle instance, but not any other object which does not implement IShape) the program will call the corresponding GetArea method.

I hope this helped even if it is not so clear: interfaces are not so easy to learn (well, as far as I am concerned, I took some time to understand inheritance and I have got to understand interfaces later because I was not able to tell them apart from abstract classes. I realised later that they were almost exactly the same thing).

Edit: there actually is a difference between an abstract class and an interface. Look this: http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class

LG
Sergey Alexandrovich Kryukov 13-Jun-15 22:14pm    
Probably many could, but why? Why would you think that a Quick Answer written specially for you should be better than many articles and books written thoroughly? Don't you think you should better read some of available literature?
—SA

1 solution

Learning interfaces in .NET framework is as simple as 1, 2, 3...


  1. Interfaces (C# Programming Guide)[^] MSDN is the best solution for Microsoft products development. I always recommend MSDN.
  2. Interfaces in C# (For Beginners)[^] CodeProject is my second choice!
  3. C# interfaces - What's the point?[^] Stack Overflow is also a great resource, unbiased solutions are provided.


Read them, and come back if you still don't understand. :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-15 22:14pm    
5ed.
—SA
Afzaal Ahmad Zeeshan 14-Jun-15 6:38am    
Thank you very much.

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