C# explicit interface implementation usage in detail

An interface is a collection of loose items that share a common function or property. The interface contains method signatures, properties, events, etc. Interfaces are used so that a class or structure can achieve multiple behaviors. Not supported in C#

Multiple inheritance

Because of the ambiguity it causes. However, many real-life objects inherit properties from more than just one type, so use interfaces instead of extension classes.

An interface consists only of signatures, not of its implementation, so any class or structure that implements an interface must provide an implementation by overridden.

What is an explicit interface implementation? When to use it?

Explicitly telling the compiler that a member belongs to that particular interface is called an explicit interface implementation.

If a class is implemented from multiple interfaces that have a method with the same signature, a call to that method will implement the same method instead of an interface-specific method. This would defeat the whole purpose of using a different interface. That’s when it’s done explicitly. With an explicit implementation, you can tell the compiler which interface’s methods you’re overloading, and you can provide different functionality for methods of different interfaces. The same is true for any other type of member (e.g., property, event).

The syntax is as follows:

class ClassName : InterfaceName
{
    returnType InterfaceName.method()
    { 
          //Your Code 
    }
}

Example 1: The program shows the use of an explicit interface implementation. Here we have two interfaces, I1 and I2, which have the same method signature, called printMethod, and return type void. Class C implements both interfaces, so we use an explicit interface implementation to distinguish between methods.

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface I1 {
  
     void printMethod();
}
  
interface I2 {
  
     void printMethod();
}
  
//class C implements two interfaces
class C : I1, I2 {
  
     //Explicitly implements method of I1
     void I1.printMethod()
     {
         Console.WriteLine( "I1 printMethod" );
     }
  
     //Explicitly implements method of I2
     void I2.printMethod()
     {
         Console.WriteLine( "I2 printMethod" );
     }
}
  
//Driver Class
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         I1 i1 = new C();
         I2 i2 = new C();
  
         //call to method of I1
         i1.printMethod();
  
         //call to method of I2
         i2.printMethod();
     }
}

The output is as follows:

I1 printMethod
I2 printMethod

Example 2: Here, we have a method for the interface Pyramid drawPyramid. The class display inherits this method and provides a pyramid that prints on the screen. We use the Explicit implementation to override the drawPyramid method.

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface Pyramid {
  
     //Method signature
     void drawPyramid();
}
  
//Implements Pyramid
class Display : Pyramid {
  
     //Using Explicit implementation
     void Pyramid.drawPyramid()
     {
         for ( int i = 1; i <= 10; i++) 
         {
             for ( int k = i; k <10; k++)
                 Console.Write( " " );
  
             for ( int j = 1; j <= (2 * i - 1); j++)
                 Console.Write( "*" );
  
             Console.WriteLine();
         }
     }
}
  
//Driver Code
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         //Create object of the class using Interface
         Pyramid obj = new Display();
  
         //call method
         obj.drawPyramid();
     }
}

The output is as follows:

         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

Example 3: We can use an explicit interface implementation for even properties and basically any other member of the interface. Here, we have Property X and Method X in the interface I1 and I2 have the same name and return type, respectively. We only use an explicit interface to implement X for methods. In this way, the compiler will use the property X unless otherwise noted.

//C# Program to show the use of
//Explicit interface implementation
using System;
  
interface I1 {
  
     //Property X
     int X
     {
         set ;
         get ;
     }
}
  
interface I2 {
  
     //Method X
     int X();
}
  
class C : I1, I2 {
  
     int x;
  
     //Implicit implementation of
     //the property
     public int X
     {
         set { x = value; }
         get { return x; }
     }
  
     //Explicit implementation of
     //the method
     int I2.X()
     {
         return 0;
     }
}
  
//Driver Code
class GFG {
  
     //Main Method
     static void Main( string [] args)
     {
         C c = new C();
         I2 i2 = new C();
  
         //Invokes set accessor
         c.X = 10;
  
         //Invokes get accessor
         Console.WriteLine( "Value of x set using X" +
                              " from I1 is " + c.X);
  
         //Call to the X method
         Console.WriteLine( "Value returned by I2.X()" +
                                     " is " + i2.X());
     }
}

The output is as follows:

Value of x set using X from I1 is 10
Value returned by I2.X() is 0