C# Interface

The interface is an important part of an object-oriented programming language. The Interface contains only the declarations of events, indexers, methods and/or properties. An Interface can not have an implementation. In C# a class or a structure can implement an Interface.

The class or struct who implement an Interface must contain the implementation of all the events, indexers, methods and/or properties whoever declared in the Interface.

Like we declare the classes, interfaces can also be declared by using the keyword interface. Following is the syntax of a C# interface

interface ICustomer
{
    //Interface body
}

Defining an Interface

The interface name is generally prefixed with an I letter. However, it is not required but it is Recommended. Basically I letter denotes an Interface. One can easily assume that a specific type is an interface by just looking at its name. In the following example, there is a simple method declaration, whose return type is void and it has no parameters.

interface IEmployee
{
    void PrintDetails();
}

You can make your own declaration of a method of any type with different parameters and return type.

By default all the members of an Interface are public. You can't put a public keyword explicitly in front of a member declaration.

Implementing an Interface

We can implement an Interface into a class, in the same way, we inherit a class. In the following example, the Customer class is implementing the ICustomer Interface.

interface IEmployee
{
    void PrintDetails();
}
 
class Employee : IEmployee
{
}

Now that the above Employee class has implemented the ICustomer Interface, it must implement all the members in the Class who are declared in the interface.

class Employee : IEmployee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public float Salary { get; set; }
    public string Address { get; set; }
 
    public void PrintDetails()
    {
        Console.WriteLine("Employee first name is  : {0} \n Employee last name is : {1} \n Employee salary is : {1}", this.FirstName, this.LastName, this.Salary);
    }
}

Interface Inheritance

In C# an Interface can inherit another Interface. So if a class or struct is implementing an interface which is inheriting another Interface then that class or struct will have to implement all the interface members who come in the entire inheritance chain.

Following is an example of Interface Inheritance.

interface IAddress
{
    void PrintAddress();
}
 
interface IEmployee : IAddress
{
    void PrintDetails();
}
 
class Employee : IEmployee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public float Salary { get; set; }
    public string Address { get; set; }
 
    public void PrintDetails() //Declared in IEmployee interface
    {
        Console.WriteLine("Employee first name is  : {0} \n Employee last name is : {1} \n Employee salary is : {2}", this.FirstName, this.LastName, this.Salary);
    }
 
    public void PrintAddress() //Declared in IAddress interface
    {
        Console.WriteLine("Employee first name is  : {0} \n Employee last name is : {1} \n Employee salary is : {2} \n Employee address is : {3}",
            this.FirstName, this.LastName, this.Salary, this.Address);
    }
}

The interface does not allow to create an instance of it. If you will try to do so, you will get a compile-time error. However, an interface reference variable can point to the object of a derived class.

IEmployee Emp = new IEmployee(); //Not possible

Explicit Interface Implementation

While implementing multiple interfaces in a class, sometimes we encounter a situation where the same name member declaration exists in different interfaces. In that situation while implementing the interface member one can not predict that which implementation belongs to a specific interface.

Let's understand the situation with an example.

interface IMark
{
    void PrintDetails();
}
 
interface IStudent
{
    void PrintDetails();
}
 
class Student:IStudent,IMark
{
    //class body
}

In the above example, IMark and IStudent both the Interfaces have PrintDetails method with same return type and it has no parameters. Now if Student class will implement this method, just one implementation is fine. You will not get any compiler error. But here one can not predict that PrintDetails implementation is of which Interface.

To handle this situation explicit implementation is used. Explicit implementation can be used just by putting interface name in front of the member implementation.

Following example demonstrate an explicit implementation.

class Student:IStudent,IMark
{
    void IStudent.PrintDetails()
    {
        throw new NotImplementedException();
    }
 
    void IMark.PrintDetails()
    {
        throw new NotImplementedException();
    }
}

Let's learn about the abstract class in the next section.