Explicit Interface Implementation

Article Overview

  • Background
  • Prerequisites
  • Why is Explicit Interface Implementation required?
  • How to do Explicit Interface Implementation?
  • How to do Default & Explicit Implementation?
  • Summary

Background

 
A class inherits from 2 interfaces and both the interfaces have the same method name. Using explicit interface implementation the class should implement the method for both the interfaces.
 
There are various situations where you need to provide the same method name to multiple interfaces. Therefore at the time of implementation/inheritance you need to specify explicitly interface implementation with method names. This is known as Explicit Interface Implementation.
 
Let us go with some practical examples to know more about the Explicit Interface Implementation as follows: 
  1. Why is Explicit Interface Implementation required?
  2. How to do Explicit Interface Implementation?
  3. How to do Default & Explicit Implementation?
Prerequisites
 
You should have a basic knowledge of Interface Implementation in C#.
 

Why is Explicit Interface Implementation required?

 
To solve the following kind of problem Explicit Interface Implementation technique is used.
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     interface ICircle  
  6.     {  
  7.         void Area();  
  8.     }  
  9.   
  10.     interface IRectangle  
  11.     {  
  12.         void Area();  
  13.     }  
  14.   
  15.     public class Shape : ICircle, IRectangle  
  16.     {  
  17.         public void Area()  
  18.         {  
  19.             Console.WriteLine("Circle/Rectangle Area");  
  20.         }  
  21.     }  
  22.   
  23.     class Program  
  24.     {  
  25.         static void Main(stri[] args)  
  26.         {  
  27.             Shape shape = new Shape();  
  28.   
  29.             ((ICircle)shape).Area();  
  30.             ((IRectangle)shape).Area();  
  31.   
  32.             Console.ReadLine();  
  33.         }  
  34.     }  
  35. }  
Output
 
Circle/Rectangle Area
Circle/Rectangle Area 
 
Here, you can see that both times the same method is called.
 
Therefore, to implement both methods separately for interface Circle and Rectangle you have to do it with Explicit Interface Implementation as follows. 
 

How to do Explicit Interface Implementation?

  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     interface ICircle  
  6.     {  
  7.         void Area();  
  8.     }  
  9.   
  10.     interface IRectangle  
  11.     {  
  12.         void Area();  
  13.     }  
  14.   
  15.     public class Shape : ICircle, IRectangle  
  16.     {  
  17.         void ICircle.Area()  
  18.         {  
  19.             Console.WriteLine("Circle Area");  
  20.         }  
  21.   
  22.         void IRectangle.Area()  
  23.         {  
  24.             Console.WriteLine("Rectangle Area");  
  25.         }  
  26.   
  27.     }  
  28.   
  29.     class Program  
  30.     {  
  31.         static void Main(string[] args)  
  32.         {  
  33.             Shape shape = new Shape();  
  34.   
  35.             ((ICircle)shape).Area();  
  36.             ((IRectangle)shape).Area();  
  37.   
  38.             Console.ReadLine();  
  39.   
  40.             //OR You can also do above like following  
  41.             //ICircle circle = new Shape();  
  42.             //IRectangle rectangle = new Shape();  
  43.   
  44.             //circle.Area();  
  45.             //rectangle.Area();  
  46.   
  47.             //Console.ReadLine();  
  48.         }  
  49.     }  
  50. }  
Output
 
Circle Area
Rectangle Area
 
Note
  • When a class explicitly implements an interface member the interface member can no longer be accessed thru class reference variable but only through the interface reference variable.
  • Access modifiers are not allowed on explicitly implemented interface members.

How to do Default & Explicit Implementation?

 
If you want to make one of the interface methods as default then implement that method as normal and the other interface method as explicit.
 
This makes the default method to be accessible thru class instance. 
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     interface ICircle  
  6.     {  
  7.         void Area();  
  8.     }  
  9.   
  10.     interface IRectangle  
  11.     {  
  12.         void Area();  
  13.     }  
  14.   
  15.     public class Shape : ICircle, IRectangle  
  16.     {  
  17.         public void Area()  
  18.         {  
  19.             Console.WriteLine("Circle Area");  
  20.         }  
  21.   
  22.         void IRectangle.Area()  
  23.         {  
  24.             Console.WriteLine("Rectangle Area");  
  25.         }  
  26.     }  
  27.   
  28.     class Program  
  29.     {  
  30.         static void Main(string[] args)  
  31.         {  
  32.             Shape shape = new Shape();  
  33.               
  34.             shape.Area();  
  35.               
  36.             ((IRectangle)shape).Area();  
  37.   
  38.             Console.ReadLine();              
  39.         }  
  40.     }  
  41. }  
Output
 
Circle Area // this is default implementation
Rectangle Area // this is explicit implementation 
 

Summary

 
Now, I believe you know more about why explicit interface implementation is required and how to implement it.


Similar Articles