C# | Explicit Interface Implementation

An Interface is a collection of loosely bounded items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors. C# doesn’t support the concept of Multiple Inheritance because of the ambiguity it causes. But a lot of real life objects inherit properties of more than just one type, therefore interfaces are used instead of extending classes.
An Interface consists only of the signatures and not its implementation, therefore, any class or struct that implements it has to provide the implementation by overriding.

What is Explicit Interface Implementation and when is it used?

Explicitly telling the compiler that a particular member belongs to that particular interface is called Explicit interface implementation.
If a class implements from more than one interface that has methods with the same signature then the call to such a method will implement the same method and not the interface specific methods. This will defeat the whole purpose of using different Interfaces. That is when Explicit implementation comes into the picture. Using explicit implementation you can tell the compiler which interface’s method you are Overloading and can provide different functionalities for methods of different interfaces. The same is the case for any other type of member like a property, event.

Syntax:

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

Example 1: This program shows the use of explicit interface implementation. Here we have two interfaces I1 and I2 that have the same method signature named printMethod with return type as void. Class C implements these two Interfaces, therefore we use explicit interface implementation to distinguish between the methods.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

I1 printMethod
I2 printMethod

Example 2: Here, we have an Interface Pyramid with a method drawPyramid. The class Display inherits this method and provides an implementation that prints a ‘*‘ pyramid on the screen. We use Explicit implementation to override drawPyramid method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

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

Example 3: We can use explicit interface implementation for even properties and basically any other members of the interface. Here, we have property X and method X in interface I1 and I2 respectively with same names and return types. We only use explicit interface implementation on method X. This way the compiler will use the property X unless specified otherwise.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Student at Padre Conceicao College of Engineering, Goa Computer Engineering

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.