C# | Explicit Interface Implementation
Last Updated :
11 Jul, 2025
An Interface is a collection of loosely bound 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.
C#
// 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();
}
}
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.
C#
// 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();
}
}
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 the 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.
C#
// 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());
}
}
Output: Value of x set using X from I1 is 10
Value returned by I2.X() is 0
Similar Reads
C# | Inheritance in interfaces C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.Important Points: If a class implements an interface, then it is nece
3 min read
C# Program To Implement IDisposable Interface IDisposable is an interface defined in the System namespace. It is used to release managed and unmanaged resources. Implementing IDisposable interface compels us to implement 2 methods and 1 boolean variable -Â Public Dispose() : This method will be called by the consumer of the object when resource
4 min read
C# Program to Implement an Interface in a Structure Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co
2 min read
C# Program to Implement Multiple Interfaces in the Same Class Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interfaceâs members will be given by the class that implements the interface implicitly or explicitly. C# allows that a sin
3 min read
Default Interface Methods in C# 8.0 Before C# 8.0 interfaces only contain the declaration of the members(methods, properties, events, and indexers), but from C# 8.0 it is allowed to add members as well as their implementation to the interface. Now you are allowed to add a method with their implementation to the interface without break
5 min read
C# Program to Demonstrate Interface Implementation with Multi-level Inheritance Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q,
3 min read