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 the implicit implementation of the interface and when to use implicit implementation of the interface in C#?
C# interface members can be implemented in two ways: implicitly and explicitly. With implicit implementation, the implementing class doesn't include the interface name before the member name, making the compiler infer the interface connection automatically.
In implicit implementation, the members are exposed as public and are accessible when the object is cast as either the concrete type or the interface type. This approach is simpler and more commonly used when there are no naming conflicts between interface methods.
Syntax
Following is the syntax for implicit interface implementation −
interface IInterfaceName {
void MethodName();
}
class ClassName : IInterfaceName {
public void MethodName() {
// implementation without interface prefix
}
}
When to Use Implicit Implementation
-
When you want the interface members to be accessible through the concrete class instance.
-
When there are no naming conflicts between multiple interfaces.
-
When you need a simpler, more straightforward implementation approach.
-
When the interface methods should be part of the class's public API.
Using Implicit Implementation
Example
using System;
interface ICar {
void DisplayCar();
void Start();
}
interface IBike {
void DisplayBike();
void Start();
}
class Vehicle : ICar, IBike {
public void DisplayCar() {
Console.WriteLine("This is a car display");
}
public void DisplayBike() {
Console.WriteLine("This is a bike display");
}
public void Start() {
Console.WriteLine("Vehicle started");
}
}
class Program {
static void Main() {
Vehicle vehicle = new Vehicle();
vehicle.DisplayCar();
vehicle.DisplayBike();
vehicle.Start();
ICar car = new Vehicle();
car.DisplayCar();
car.Start();
IBike bike = new Vehicle();
bike.DisplayBike();
bike.Start();
}
}
The output of the above code is −
This is a car display This is a bike display Vehicle started This is a car display Vehicle started This is a bike display Vehicle started
Limitation: Method Name Conflicts
Implicit implementation has a limitation when multiple interfaces have methods with the same signature. In such cases, only one implementation can exist, which may not provide the specific behavior each interface requires.
Example with Conflict Resolution
using System;
interface IDrawable {
void Draw();
}
interface IPrintable {
void Draw(); // Same method name as IDrawable
}
// This approach works but both interfaces share the same implementation
class Document : IDrawable, IPrintable {
public void Draw() {
Console.WriteLine("Drawing/Printing the document");
}
}
class Program {
static void Main() {
Document doc = new Document();
doc.Draw(); // Calls the single implementation
IDrawable drawable = new Document();
drawable.Draw(); // Same implementation
IPrintable printable = new Document();
printable.Draw(); // Same implementation
}
}
The output of the above code is −
Drawing/Printing the document Drawing/Printing the document Drawing/Printing the document
Comparison: Implicit vs Explicit Implementation
| Aspect | Implicit Implementation | Explicit Implementation |
|---|---|---|
| Syntax | public void Method() |
void IInterface.Method() |
| Access via Class | Yes | No |
| Access via Interface | Yes | Yes |
| Method Name Conflicts | Cannot resolve individually | Can resolve individually |
| Visibility | Public | Only through interface |
Conclusion
Implicit interface implementation in C# is the simpler approach where interface members are implemented as public methods accessible through both the class and interface references. Use implicit implementation when there are no method name conflicts and you want the interface methods to be part of the class's public API.
