Open In App

C# | Interface

Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface’s members will be given by class who implements the interface implicitly or explicitly.

Syntax for Interface Declaration:



interface  <interface_name >
{
    // declare Events
    // declare indexers
    // declare methods 
    // declare properties
}

Syntax for Implementing Interface:

class class_name : interface_name

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the members in the interface are declared with the empty body and are public and abstract by default. A class that implements interface must implement all the methods declared in the interface.



Advantage of Interface:

  • It is used to achieve loose coupling.
  • It is used to achieve total abstraction.
  • To achieve component-based programming
  • To achieve multiple inheritance and abstraction.
  • Interfaces add a plug and play like architecture into applications.

  • Article Tags :
    C#