I am working on learning C# in depth. I am mostly confused by the frequent implementation of interfaces. I always read that this class implements this interface. For instance, SqlConnection class implements IDbConnection. What is the benefit for developers in this case?
-
4read this http://stackoverflow.com/questions/240152/why-would-i-want-to-use-interfaces try google next time. – snajahi Apr 04 '14 at 21:44
-
1One advantage of interfaces is, that you can inherit a class from multiple interfaces where you can only have one base class. – Rand Random Apr 04 '14 at 21:49
2 Answers
An interface contains definitions for a group of related functionalities that a given type must implement (a sort of Method Signature Contract). It does not, however, guarantee the specific behavior of those implementations.
Interfaces are particularily useful as they allow the programmer to include behavior from multiple sources in programming languages that do not support multiple inheritance of classes like C#.

- 9,708
- 5
- 58
- 67
-
Sir, I understand the fact that interfaces only contain definitions. But i am confused with why Microsoft implements interfaces heavily and what is the advantage for the developers. I mean how can i benefit from the IDbConnection interface. – Mohammad Ajmal Amirzad Apr 04 '14 at 21:59
the interfacing is based on object-oriented principles, e.g. see SOLID. You should not rely on implementation of other classes you're working with - it should be sufficient for you to know only what they do and what they should return. A good example with the SqlConnection would be that you may be able to change the DB you are using quite simply (to e.g. MySQL or Oracle) by changing the implementation on just one place, providing that your code is correctly using the interfaces and propagating the instances.

- 2,867
- 16
- 28