Interface in C#

In C# an interface is user-defined reference types data types. This article describes basic overview of interface or how to use interface in C#. Summary of the article:

  • What is Interface?
  • Why we should use Interface?
  • Declaring Interface
  • Example of Interface
  • Features/Properties of Interface

What is Interface?
An interface is like a class but has no implementation. It contains only the declarations of events, methods, properties, indexers. Because, it is inherited by classes and struts, that must provide an implementation for each interface member declared.

Interface is a template that contains only the signature of methods. This method consists of the numbers of parameters, the type of parameters, and the order of parameters. An interface has no implementation on its own. Because it contains only the definition of methods and the method has no method body. In addition, we cannot instantiate an interface.

 

Why we should use Interface?
Hope everyone is quite familiar with OOP concepts (Class, Object, Encapsulation, Inheritance, Polymorphism, etc). We use different features of OOP in programming which makes our works simple and efficient. Inheritance is one of the great features of OOP.  Where, one class can obtain the features of other classes. Parent class is called based class and the child class is called derived class. Its help us to reuse the same codes and eliminates the use of redundant code.

Like other programming language C# does not support multiple inheritances directly. In .NET, a class cannot inherit more than one class. But we can use multiple inheritances through interface.

For example consider the following C# codes where we have two classes (MyClassA, MyClassB) and we want to inherits class MyClassA and MyClassB in class MyClass.

public class MyClassA
{
    //members or methods               
}

public class MyClassB
{
    //members or methods
}

public class MyClass : MyClassA, MyClassB
{

}

Here, MyClass is parent class and MyClassA and MyClassB is child class. Now if we compile the codes it will through en error [Class ‘…Class’ cannot have multiple base classes:] Which means multiple inheritances is not supported by C#. We can easily overcome it by using interface. An implementation of above problem is described below.

Declaring Interface
Generally an Interface is defined or declared using the interface keyword. It is similar to class declaration. By default its statements are public. Following is an example of an interface declaration:

public interface ISampleInterface
{
    // interface members
    void SampleMethod();
}

Example of Interface
A good example of interface is given bellow:

interface MyClassA
{
    string MyMethodA();
}

interface MyClassB
{
    string MyMethodB();
}

public class MyClass : MyClassA, MyClassB
{
    public string MyMethodA()
    {
        return "my method A";
    }

    public string MyMethodB()
    {
        return "my method B";
    }

    public string MyMethod()
    {
        return MyMethodA();
    }
}

Features/Properties of Interface
The Features of an Interface or Properties of Interface are as follows:

  • An interface is used to implement multiple inheritances in code. This feature of an interface is quite different from that of abstract classes because a class cannot derive the features of more than one class but can easily implement multiple interfaces.
  • It defines a specific set of methods and their arguments.
  • The variables in interface must be public or static and the methods must be public and abstract.
  • When a class implement an interface then it is necessary to implement all of its methods.
  • An interface can derive from one or more interface. That’s means Interfaces may also be inherited by other interface.

That’s all about C# interface.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *