Interface in C#

Interface in C#

Why Use Interface?

In programming, we use interface when we have some same functionalities in different classes, but all the functionalities are performed differently so to solve this issue we use interface.

The interface is implemented by different classes and every class that implements an interface must have a definition of all the abstract methods of the attributes and functions of that interface.

The interface tells the functionality i.e. Signature of the functions and the class which implements the interface perform the functionality i.e. Provide definition/body of the abstract methods of the interface. How for this interface? If we have different animals classes, and we know every animal eat, but all the animals do not eat the same things so we just declare the eat function in the interface and all the classes which implement that interface must define them.


Above is the simple syntax of an interface. The interface has functions and attributes, but not their body and if we try to define them within the interface, then it generates an error.

Multiple Inheritance in Interface:

Multiple inheritances are also possible in the case of interfaces. Which we can’t do in classes i.e. One class can’t extend more than one classes, but a class can implement more than one interface.


Now we will see how a class implements an interface.

And as in above example the class dogs implements interface animals, so it is compulsory for dog class to define the eat() function in its body.

The interface is the same as an abstract class, but an abstract class has both abstract and non-abstract function, and the interface has the only abstract function, and more likely we have no need to define them as an abstract function or pure virtual function as in C++.

Understanding Interface

The following example shows the working of the interface