C# Explicit Interface Implementation

Summary: in this tutorial, you’ll learn about C# explicit interface implementation and how to use it effectively.

Introduction to C# explicit interface implementation

In C#, a class can implement an interface in two ways: implicitly and explicitly. By default, C# uses the implicit implementation. For example:

interface ILogger
{
    void Log(string message);
}

class ConsoleLoger : ILogger
{
    public void Log(string message) => Console.WriteLine(message);
}

class Program
{
    public static void Main(string[] args)
    {
        var logger = new ConsoleLoger();
        logger.Log("Hello");
    }
}Code language: C# (cs)

In this example, we define an ILogger interface with one method called Log, which logs a message. Then, we define a ConsoleLogger class that implements the ILogger interface. The ConsoleLogger class implements the Log method that logs a message to the console.

In the Main() method of the Program class, we create a new instance of the ConsoleLogger class and log a message to the console.

Unlike the implicit interface implementation, an explicit interface implementation requires you to specify the interface name when implementing the method. For example:

interface ILogger
{
    void Log(string message);
}

class ConsoleLoger : ILogger
{
    void ILogger.Log(string message) => Console.WriteLine(message);
}

class Program
{
    public static void Main(string[] args)
    {
        ILogger logger = new ConsoleLoger();
        logger.Log("Hello");
    }
}Code language: C# (cs)

In this example, the ConsoleLogger implements the ILogger interface explicitly. Therefore, we prefix the interface name before the method name, separated by a dot (.). Also, we don’t use an access modifier (public) for the Log method in the ConsoleLogger.

In the Main() method of the Program class, we can only access the Log method via the ILogger interface. The reason is that the Log method belongs to the ILogger interface, not the ConsoleLogger class even though we implement it in the ConsoleLogger class.

This is also the reason why the Log method doesn’t have the public access modifier. In other words, you cannot call the Log method via an instance of the ConsoleLogger class.

Therefore, the following code will not be compiled and result in an error:

ConsoleLoger logger = new ConsoleLoger();
logger.Log("Hello"); // errorCode language: C# (cs)

When to use C# explicit interface implementation

The explicit interface implementation allows you to prevent naming conflicts between multiple interfaces.

Since a class can implement multiple interfaces and these interfaces may have members with the same name. To prevent the naming conflict, you can use the explicit interface implementation.

The following example demonstrates how to use explicit interface implementation:

interface IWritable
{
    void Write(string message);
}

interface ISavable
{
    void Write(string message);
}

class TextFile : IWritable, ISavable
{
    public string FileName  {  get;  set; }
    public TextFile(string filename)
    {
        FileName = filename;
    }
    void IWritable.Write(string message) => Console.WriteLine(message);
    void ISavable.Write(string message) => File.WriteAllText(FileName, message);
}

class Program
{
    public static void Main(string[] args)
    {
        var textFile = new TextFile(@"C:\temp\test.txt");
        ISavable savable = textFile;
        savable.Write("Hello");

        IWritable writable = textFile;
        writable.Write("Hello");

    }
}Code language: C# (cs)

How it works.

First, define the IWritable and ISavable interface that has the same method Write.

Second, define the TextFile class that implements both IWritable and ISavable interfaces explicitly.

Third, create a new instance of the TextFile, and call the Write method of the interfaces via ISavable and IWritable interface respectively.

The program writes the Hello message to the console and to a C:\temp\test.txt file.

Summary

  • Explicit interface implementation provides you with a way to implement interface methods with a specific interface prefix (interfaceName.methodName).
  • Explicit interface implementation is useful when you have a class that implements multiple interfaces with the same method name, or when you want to avoid naming conflicts between interface and class methods.
  • Explicit interface implementation ensures that the method can only be accessed through the interface it belongs to.
Was this tutorial helpful ?