What is Interface?
- An interface can contain signatures (declarations) of the Methods, Properties, Indexers and Events.
- The implementation of the methods is done in the class that implements the interface.
- A Delegate is a type that can't be declared in an interface. You can either use an event (if appropriate) or declare a delegate outside the interface but in the same namespace.
- Interfaces in C# provides a way to achieve runtime polymorphism. Using interfaces, we can invoke functions from various classes through the same Interface reference, whereas using virtual functions we can invoke functions from various classes in the same inheritance hierarchy through the same reference.
- An interface can inherit from one or more base interfaces.
- A class that implements an interface can explicitly implement members of that interface.
- An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
Purposes of Interfaces
- Create loosely coupled software.
- Support design by contract (an implementer must provide the entire interface).
- Allow for pluggable software.
- Allow objects to interact easily.
- Hide implementation details of classes from each other.
- Facilitate reuse of software.
No comments:
Post a Comment