C# Programming
Interfaces in C#
Implicit Implementation
Explicit Implementation
Software Development

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

csharp
1public interface IExampleInterface
2{
3    void ExampleMethod();
4    int ExampleProperty { get; set; }
5}

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

csharp
1public interface IDrive
2{
3    void StartEngine();
4}
5
6public class Car : IDrive
7{
8    // Implicitly implementing the interface method
9    public void StartEngine()
10    {
11        Console.WriteLine("The engine has started.");
12    }
13}

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

csharp
1public interface IFly
2{
3    void Navigate();
4}
5
6public interface ISail
7{
8    void Navigate();
9}
10
11public class AmphibiousVehicle : IFly, ISail
12{
13    // Explicit implementation for IFly.Navigate
14    void IFly.Navigate()
15    {
16        Console.WriteLine("Flying navigate.");
17    }
18
19    // Explicit implementation for ISail.Navigate
20    void ISail.Navigate()
21    {
22        Console.WriteLine("Sailing navigate.");
23    }
24}

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.

FeatureImplicit ImplementationExplicit Implementation
AccessibilityAccessible from class instanceNot accessible directly through class Cast to interface required
Method Signature CollisionNot suitable for resolutionUseful for resolving collisions
Code ReadabilityMore intuitiveLess intuitive due to casting
Use CaseGeneral purpose implementationSpecific 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.


Course illustration
Course illustration

All Rights Reserved.