1. OnlineBuff

Interface in C# using an example

What is an Interface?

An interface looks like a class but has got no implementation. In an interface we cannot do any implementation but we can declare signatures of properties, methods, delegates and events.

Implementation part is been handle by the class that implements the interface. Implemented interface enforces the class like a standard contract to provide all implementation of interface members.

We can implement single interface or multiple interfaces to the class. By default interfaces are public.

We declare interface by using "interface" keyword.

 
interface IEmployee{

void DisplayEmployeeDetails();

}

Above syntax shows simple demonstration of interface "IEmployee" with signature of a method "DisplayEmployeeDetails". Now let's implement the same interface to a class.

 
class clsEmployee : IEmployee
{
          public void DisplayEmployeeDetails(){
	 Console.WriteLine(“Displaying employee details…”);
           }

}

As you see in our above snippet of code that implementation of an interface method signature is done by the class which implemented that interface.

Benefits of using an interface

Implemented interface enforces the class like a standard contract to provide all implementation of interface members.

In architecting the project we can define the proper naming conventions of methods, properties in an interface so that while implementing in a class we use the name conventions.

We can use the "interface" as a pointer in the different layers of applications.

We can use an interface for implementing run time polymorphism.

Various Forms of implementing interface

There are two of ways of implementing interfaces

Explicit
Implicit

Explicit interface implementation


To implement an interface explicitly we have to define an interface name followed by (".") dot operator then the method name.

Sample as shown in below snipped code.

 

public class Employee : IEmployee
{
        void IEmployee.DisplayEmployeeDetails()
        {
            Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");
        }
}

When you put a dot operator after an interface name you will see all the signatures defined in an interface.

In which scenario we use explicit interface implementation

When we have two different interfaces with same method name then in that scenario we can use explicit interface implementation.

Example of Explicit Interface

 
public interface IEmployee{
    void DisplayEmployeeDetails();
}

public interface ICompany{
    void DisplayEmployeeDetails();
}

In the above code we have two different interfaces "IEmployee" and "ICompany" with same method name. Now let's implement these two interfaces to a class "clsEmployee" as shown in below snippet of code.

 
public class clsEmployee : IEmployee,ICompany
{

//class code goes here

}

In order to implement an interface explicitly we have to define the interface name followed by (".") dot operator before method name as shown in below snipped code.

 
public class Employee : IEmployee,ICompany
{
    void ICompany.DisplayEmployeeDetails(){
            Console.WriteLine("ICompany Employee Name --> Questpond and Employee Code --> 009");
    }
    void IEmployee.DisplayEmployeeDetails(){
            Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");
    }
}

Console Application

Finally let's create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a Console Application program.

 
class Program{

  static void Main(string[] args)
 {
    IEmployee IEmp = new clsEmployee();
    IEmp.DisplayEmployeeDetails();

    ICompany IComp = new clsEmployee();
    IComp.DisplayEmployeeDetails();
 }

}

Let's run the application (Ctrl + F5) and display the output as shown it below.

 

Implicit interface implementation

To implement an interface implicitly we have to implement the interface name before the class using colon ":" operator as shown in below snipped code.

 
public class Employee : IEmployee
{
        public void DisplayEmployeeDetails()
        {
            Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");
        }
}

When you put a colon after a class name you will see the custom defined and as well as system defined interfaces in the program.

Example of Implicit Interface

 
    interface IEmployee
    {
        void DisplayEmployeeDetails();
    }

Above code shows simple demonstration of an interface "IEmployee" with signature of a method "DisplayEmployeeDetails".

Now let's create a class "Employee" and implement the interface implicitly as shown in below snippet of code.

 
public class Employee : IEmployee
{
        public void DisplayEmployeeDetails()
        {
            Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");
        }
}

Console Application

Finally let's create the objects of an interface "IEmployee" in our main method of a Console Application program.

 
public class Program{

 static void Main(string[] args)
 {
    IEmployee IEmp = new clsEmployee();
    IEmp.DisplayEmployeeDetails();
 }

}

Let's run the application (Ctrl + F5) and display the output as shown it below.

Thank you for reading my article if you have any doubts or any query regarding interface kindly let me know through your valuable comments.

Author: Gurunatha Dogi

is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more. Follow me @ and twitter.... read more

Comments

64x64
By Pavan on 2016-05-19
good
64x64
By Meharoof KK on 2014-09-30
Good Article..very helpful........
64x64
By Hiranya on 2014-06-06
Excellent article!!! Keep rocking!!!!!!@@@@
64x64
By Yogesh Chandra Upreti on 2014-05-15
Nice Article. Please clarify when we have need to use Interface OR abstract class give some good examples.
64x64
By Yogesh Chandra Upreti on 2014-05-15
Nice Article. Please clarify when we have need to use Interface OR abstract class give some good examples.
64x64
By Abirami on 2014-04-23
thank you...for the great post
64x64
By Sirish on 2013-11-12
Thank you
64x64
By Abhishek Sharma on 2013-09-22
hello Sir this artical was nice about interface but i doesn't understand about Explicit interface and implicit interface Cen you Express another Example about on that..
64x64
By Nirmala on 2013-07-18
Could you please an article on "Delegates and Events" in C# with suitable example. It will be very helpful. Thanks in advance
64x64
By Akil on 2013-07-10
If a class implement 2 interface,(If they both class will have same method name) then how you implement the implicit interface.. PLease waiting for your reply?

Add a Comment