Implicit And Explicit Interface Implementation In C#

Implicit Implementation

Let's take an example to understand, what implicit and explicit implementation of interface is.

I am going to create an interface named with “IEmployee” and a class named with “Employee”.

When the class implements the interface member, it does not need to specify the interface name with the member name. We can simply implement it.

  1. interface IEmployee  
  2. {  
  3.    void Greeting();  
  4. }  
  5.   
  6. public class Employee : IEmployee  
  7. {  
  8.    public void Greeting()  
  9.    {  
  10.       Console.WriteLine("This is implicitly implementation of Interface");  
  11.    }  
  12. }  
See in the above code, when class implement the interface method “Greeting()”, it doesn’t specify the interface name.

When we need to access this method, we can create the instance of the class and when we pass object, then it gives the name of implemented method. See the following code.
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       Employee emp = new Employee();  
  6.       emp.Greeting();  
  7.       Console.ReadLine();  
  8.    }  
  9. }  
Explicit Implementation

Sometimes, we get a situation where a class implemented the multiple interface and those interfaces contain the same name method. When class is going to implement the interface members, they don’t differentiate between the method, because of the method name or same in both the interfaces.

For resolving this issue we need to specify the name of the interface at the time of implementation explicitly.

Let's take an example to understand what explicit implementation of interface is. I am going to create one more interface “IManager” which will be implemented by Employee class. IManager contains the same name method as IEmployee contains. So, the question is that how we will implement the IManager member method. Have a look in the following example.
  1. using System;  
  2.   
  3. namespace ConsoleDemo  
  4. {  
  5.   
  6.     interface IEmployee  
  7.     {  
  8.         void Greeting();  
  9.     }  
  10.   
  11.     interface IManager  
  12.     {  
  13.         void Greeting();  
  14.     }  
  15.   
  16.     public class Employee : IEmployee, IManager  
  17.     {  
  18.         void IEmployee.Greeting()  
  19.         {  
  20.             Console.WriteLine("This is greeting for Employee");  
  21.         }  
  22.         void IManager.Greeting()  
  23.         {  
  24.             Console.WriteLine("This is greeting for Manager");  
  25.         }  
  26.     }  
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             IEmployee emp = new Employee();  
  32.             emp.Greeting();  
  33.   
  34.             IManager man = new Employee();  
  35.             man.Greeting();  
  36.               
  37.             Console.ReadLine();  
  38.   
  39.         }  
  40.     }  
  41. }  
Here you should notice the following two things.

 

  1. Access modifier has been removed from the Greeting() method.
  2. The Greeting() method name showed only with the interface name [explicitly].

One more thing, you have noticed here that when we need to access the implemented method using instance of class, we use the interface name for creating the instance of the class.

  1. IEmployee emp = new Employee();  
  2. emp.Greeting();  
  3.   
  4. IManager man = new Employee();  
  5. man.Greeting();  
So, we see that when you are using the two or more interfaces which have same name method and they implemented by the same class, so method signature in the interface clashes. Therefore, in that scenario you need to specify the name of the interface at the time of implementation.

Thanks for reading and I hope you enjoyed this article.

 

Next Recommended Readings