C Interfaces. Implicit implementation versus Explicit implementation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction to C# Interfaces
In C#, interfaces are like contracts that define what methods or properties a class should implement. They do not contain any implementation themselves but provide a way to enforce certain functionalities across different classes, leading to more organized and modular code design.
What is an Interface?
An interface in C# can contain declarations for methods, properties, events, or indexers. The class or struct that implements the interface must implement all of its members. Interfaces are declared using the interface keyword and can inherit from multiple interfaces, allowing for a versatile design pattern.
Syntax
Implicit vs. Explicit Implementation
C# provides two primary ways to implement an interface: implicit and explicit. Understanding both can help developers decide which is suitable for their use case.
Implicit Implementation
With implicit implementation, the interface members are implemented in a class with the same name and signature as declared in the interface. This type of implementation is straightforward and preferred when the method should be accessible to its interface type and class type both.
Example
Here, the StartEngine method is accessible through both the Car class instances and IDrive interface references.
Explicit Implementation
In explicit implementation, the member declarations contain the name of the interface they are implementing. This is useful when a class needs to implement multiple interfaces that might have methods with the same signature, allowing for distinctive implementations for each.
Example
In this example, AmphibiousVehicle implements Navigate differently depending on whether it is being used as an IFly or ISail. The downside of explicit implementation is that members implemented this way are not accessible directly using the class instance. You must cast to the interface type to access these members.
When to Use Each
- Implicit Implementation:
- Recommended when there's no method signature collision.
- Suitable for public API methods that should be accessible from both the class and interface reference.
- Easy to understand and use due to direct access through class instances.
- Explicit Implementation:
- Necessary when multiple interfaces have member name conflicts.
- Ideal for internal implementations that need not be exposed through class instances.
- Useful when implementing interface methods that are not intended for public access.
Advantages of Using Interfaces
- Abstraction: Interfaces allow for defining the "what" and "how" independently.
- Reusability: Interfaces promote reusability across different classes since the same interface can be implemented by unrelated classes.
- Loose Coupling: Interfaces provide a level of indirection that results in loose coupling, making systems easier to maintain and evolve.
- Multiple Inheritance: In C#, classes cannot inherit from more than one class, but they can implement multiple interfaces providing a way to implement multiple inheritance.
Key Differences Table
Below is a comparative overview of implicit and explicit implementation to aid in determining the best approach for your needs.
| Feature | Implicit Implementation | Explicit Implementation |
| Accessibility | Accessible from class instance | Not accessible directly through class Cast to interface required |
| Method Signature Collision | Not suitable for resolution | Useful for resolving collisions |
| Code Readability | More intuitive | Less intuitive due to casting |
| Use Case | General purpose implementation | Specific use cases, especially with multiple interfaces having same member names |
Conclusion
Understanding and using C# interfaces effectively can drastically improve code architecture and maintainability. By leveraging implicit and explicit implementations, developers can avoid potential conflicts, enable code reuse, and provide clean abstractions, ultimately leading to robust and scalable applications.

