Interfaces in C#.
Interface is a
contract that defines the
signature of the functionality
.
So if a class is implementing a interface it says to the outer world, that
it provides specific behavior.
Example if a class is implementing ‘
Idisposable
’ interface that means it has a functionality to release unmanaged resources.
Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
- Single Class can implement multiple interfaces.
- If a class implements a interface then it has to provide
implementation to all its methods.
Ref:
an interface[
^]
For more details:
http://msdn.microsoft.com/en-us/library/ms173156.aspx[
^]
why multiple inheritance is not possible in C#
The short answer is: because the language designers decided not to.
Multiple inheritance
causes problems, if one or more of the parent classes defines members with same identifier.
You would have to decide which parent to use to handle each method call, and the operation of class can be then very different than intended.
Ref:
Multiple Inheritance not allowed in Java or C#[
^]
There are several reasons haven't provided a
baked-in, verifiable, CLS-compliant version
of multiple implementation inheritance:
1. Different languages actually have different expectations for how MI works.
For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.
2. The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?
3. Multiple implementation inheritance injects a lot of
complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.
Ref:
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx[
^]