Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is explicit implementation and when to use in the interface in C#?
Explicit interface implementation in C# allows a class to implement interface members in a way that they can only be accessed through the interface reference, not through the class instance directly. This is particularly useful when a class implements multiple interfaces that have members with the same signature.
Syntax
Following is the syntax for explicit interface implementation −
interface IInterface1 {
void Method();
}
class MyClass : IInterface1 {
void IInterface1.Method() {
// explicit implementation
}
}
Note that explicit implementations do not use access modifiers like public or private.
When to Use Explicit Implementation
Explicit interface implementation is used in the following scenarios −
Name conflicts − When a class implements multiple interfaces with members having the same signature.
Interface segregation − When you want to hide interface members from direct access through the class instance.
Version compatibility − When maintaining backward compatibility while adding new interface members.
Resolving Name Conflicts
Example
using System;
interface ICar {
void Display();
}
interface IBike {
void Display();
}
class ShowRoom : ICar, IBike {
void ICar.Display() {
Console.WriteLine("This is a Car");
}
void IBike.Display() {
Console.WriteLine("This is a Bike");
}
}
class Program {
static void Main() {
ShowRoom showroom = new ShowRoom();
// Access through interface references
ICar car = showroom;
car.Display();
IBike bike = showroom;
bike.Display();
// Direct casting also works
((ICar)showroom).Display();
((IBike)showroom).Display();
}
}
The output of the above code is −
This is a Car This is a Bike This is a Car This is a Bike
Mixing Explicit and Implicit Implementation
Example
using System;
interface IDrawable {
void Draw();
void GetInfo();
}
interface IPrintable {
void Print();
void GetInfo();
}
class Document : IDrawable, IPrintable {
// Implicit implementation - accessible directly
public void Draw() {
Console.WriteLine("Drawing the document");
}
public void Print() {
Console.WriteLine("Printing the document");
}
// Explicit implementations for conflicting method
void IDrawable.GetInfo() {
Console.WriteLine("Drawable info: Vector graphics");
}
void IPrintable.GetInfo() {
Console.WriteLine("Printable info: High resolution");
}
}
class Program {
static void Main() {
Document doc = new Document();
// Direct access to implicit implementations
doc.Draw();
doc.Print();
// Access explicit implementations through interface references
IDrawable drawable = doc;
drawable.GetInfo();
IPrintable printable = doc;
printable.GetInfo();
}
}
The output of the above code is −
Drawing the document Printing the document Drawable info: Vector graphics Printable info: High resolution
Key Rules for Explicit Implementation
| Rule | Description |
|---|---|
| No access modifiers | Explicit implementations cannot use public, private, etc. |
| Interface reference required | Can only be called through interface reference or casting |
| Cannot be virtual | Explicit implementations cannot be overridden in derived classes |
| Full interface name | Must use the complete interface name in the method signature |
Conclusion
Explicit interface implementation in C# provides a way to resolve naming conflicts when implementing multiple interfaces and allows you to hide interface members from direct class access. Use explicit implementation when you need to differentiate between interface methods with the same signature or want to encapsulate interface-specific behavior.
