Explicit Interface Implementation & default interface methods in C#

Introduction

When working with interfaces in C#, explicit interface implementation is a powerful feature that allows you to implement interface members in a way that hides them from the public interface of the class. This can be particularly useful when you have members with the same signature from multiple interfaces, or when you want to hide certain members from the consuming code.

What is Explicit Interface Implementation?

Explicit interface implementation allows a class to implement members of an interface without exposing them as public members of the class. Instead, these members can only be accessed through an instance of the interface. This can help to avoid naming conflicts and can also provide a way to implement multiple interfaces with the same member names.

Syntax of Explicit Interface Implementation

To explicitly implement an interface member, you need to specify the interface name before the member name. Here’s the syntax.

interface IMyInterface
{
    void MyMethod();
}
class MyClass : IMyInterface
{
    void IMyInterface.MyMethod()
    {
        // Implementation of the method
    }
}

In this example, MyMethod is implemented explicitly. It can only be accessed through an instance of IMyInterface, not directly through an instance of MyClass.

Default Interface Methods

Starting with C# 8.0, interfaces can now provide default implementations for their members. This allows you to add new methods to an interface without breaking existing implementations. Here’s an example:

public interface IControl
{
    void Paint() => Console.WriteLine("Default Paint method");
}
public class SampleClass : IControl
{
    // Paint() is inherited from IControl.
}

In this example, IControl defines a default implementation for the Paint method. The SampleClass class inherits this method automatically, so there’s no need to explicitly implement it unless you want to provide a different implementation.

When to Use Explicit Interface Implementation

There are a few scenarios where explicit interface implementation can be particularly useful.

  1. Avoiding Name Conflicts: If your class implements multiple interfaces that have members with the same signature, explicit interface implementation allows you to provide different implementations for each interface.
  2. Hiding Members: If you want to hide certain members from the public interface of the class and only make them accessible through the interface, you can use explicit implementation.
  3. Providing Specific Implementations: Sometimes, you might want to provide different implementations for different interfaces, even if they have the same member names.

Example: Avoiding Name Conflicts

Consider the following example where two interfaces have members with the same name.

interface IFirstInterface
{
    void CommonMethod();
}
interface ISecondInterface
{
    void CommonMethod();
}
class MyClass : IFirstInterface, ISecondInterface
{
    void IFirstInterface.CommonMethod()
    {
        Console.WriteLine("First interface method");
    }
    void ISecondInterface.CommonMethod()
    {
        Console.WriteLine("Second interface method");
    }
}

In this example, MyClass implements CommonMethod from both IFirstInterface and ISecondInterface explicitly. This allows the class to provide different implementations for each interface.

Accessing Explicitly Implemented Members

To access an explicitly implemented member, you need to cast the instance of the class to the interface type.

MyClass obj = new MyClass();
((IFirstInterface)obj).CommonMethod(); // Output: First interface method
((ISecondInterface)obj).CommonMethod(); // Output: Second interface method

Conclusion

Explicit interface implementation in C# is a powerful feature that can help you manage naming conflicts and control the visibility of interface members. By understanding how and when to use explicit implementation, you can write more robust and maintainable code. This feature is particularly useful in complex scenarios where multiple interfaces need to be implemented in a single class. Additionally, with the introduction of default interface methods in C# 8.0, interfaces have become even more flexible, allowing for the addition of new methods without breaking existing implementations.