C#: Implicit and Explicit Implementation of Interfaces

Normally an interface can be implemented in a class in a normal way, implicit implementation. Sometimes, we may have the same method name in different interfaces. If this is the case, we need to do an explicit implementation of the interface method. The main use of an explicit implementation is to avoid the ambiguities between the class or method name. In order to do that the interface name is put before that interface's method. The following example shows the implicit and explicit implementation:
  1. namespace ConsoleApplicationC  
  2. {  
  3.     // 2 interfaces with same method name  
  4.     interface IintegerAdd  
  5.     {  
  6.         void Add();  
  7.     }  
  8.     interface IfloatAdd  
  9.     {  
  10.         void Add();  
  11.         void Multiply();  
  12.     }  
  13.   
  14.     // We implement Both interfaces  
  15.     class ArithmeticOperation : IintegerAdd, IfloatAdd  
  16.     {  
  17.         // Implicit  Implementation : There is no name collision so we can implement implicitly  
  18.         // NOTE : public modifier MUST here  
  19.         public void Multiply()  
  20.         {  
  21.             float a = 1.5f, b = 2.5f;  
  22.             Console.WriteLine("Float Multiplication is:" + a * b);  
  23.         }  
  24.   
  25.         // Explicit Implementation :  Explicitly tell the compiler that we implement the interface IintegerAdd  
  26.         void IintegerAdd.Add()  
  27.         {  
  28.             int a = 10, b = 20;  
  29.             Console.WriteLine("Integer Addition Is:" + (a + b));  
  30.         }  
  31.   
  32.         // Explicit Implementation :  Explicitly tell the compiler that we implement the interface IfloatAdd  
  33.         void IfloatAdd.Add()  
  34.         {  
  35.             float a = 1.5f, b = 2.5f;  
  36.             Console.WriteLine("Float Addition Is:" + (a + b));  
  37.         }  
  38.     }  
  39.     class Program  
  40.     {  
  41.         static void Main(string[] args)  
  42.         {  
  43.             ArithmeticOperation objA = new ArithmeticOperation();  
  44.             IintegerAdd iobj = (IintegerAdd)objA;  
  45.             iobj.Add();  
  46.             IfloatAdd fobj = (IfloatAdd)objA;  
  47.             fobj.Add();  
  48.             Console.ReadKey();  
  49.         }  
  50.     }  

 Output:

 Integer Addition Is:30
 Float Addition Is:4
 
The purpose of "Explicit" implementation of an Interface
There are 2 purposes of explicit implementation of an interface. First, to avoid name collision between interface methods. That is, if you are going to create a class library, there may be a chance to use the same name in several places. At that time "explicit" implementation comes to the rescue. Secondly, you cannot access that implemented method using the object of the class directly. Instead you typecast it as an interface reference, then you can access it. This is because the C# compiler is unable to determine which one the user wants to call.
In another way, you need to add a "public" access specifier to the interface's implemented method in a class. But if you "explicitly" implement the interface then you can change this one. That means, it will look like a private method. But with one exception, you can access that explicit implemented method using the reference of the interface. Please have a look at the following code for a detailed explanation.
  1. interface ISample  
  2.     {  
  3.         void method1();  
  4.         void method2();  
  5.     }  
  6.     interface IAnotherSample  
  7.     {  
  8.         void method1();  
  9.     }  
  10.     class A : ISample, IAnotherSample  
  11.     {  
  12.         // Explicit implementation avoid name collision  
  13.         void ISample.method1()  
  14.         {  
  15.   
  16.         }  
  17.         void IAnotherSample.method1()  
  18.         {  
  19.         }  
  20.         // Implicit implementation  
  21.         public void method2()  
  22.         {  
  23.   
  24.         }  
  25.   
  26.     }  
  27.     // Inherit Class A  
  28.     class B : A  
  29.     {  
  30.         // Create Object for class A  
  31.         A obj = new A();  
  32.         B()  
  33.         {  
  34.             // You can call method2  
  35.             obj.method2();  
  36.             // Error: You cannot call method1 Since Explicit implementation  
  37.             //obj.method1();  
  38.             // But you can access it by Interface Reference  
  39.             ((ISample)obj).method1();  
  40.         }  
  41.     } 
Finally, one more point or little pros is that an explicitly implemented interface's methods are not listed in the Visual Studio IDE's intellisense.
 
 
 
Happy Coding!


Similar Articles