Explicit Interface Implementation in .NET is a language feature that, not being very commonly used, can be very handy in some situations.

In most scenarios, everyone implicitly implements interfaces, because it’s what they are used to and it’s what they find in examples about interface definition and loose coupling patterns. But implicit implementation is not the only way, nor the best. It always depends on the scenario itself.

Anyway, our interface will never depend upon the kind of implementation we are going to use. Every interface can be implemented either way.

In this article, we’re gonna see what is, how it’s done, what can we use it for, and I’ll give you a project with some examples so you can start rockin!

How do we do it?

I’m going to show you now how we would do a explicit interface implementation in our classes.

First of all we define a interface like any other we’ve done before

public interface ITest
{
    string TestMethod();
}

The regular implicit interface implementation we use normally, would look like this:

public class MyImplicitClass : ITest
{
    public string TestMethod()
    {
        // Here I do things ....
    }
}

But in this case, we’re gonna do a explicit implementation, and it reads like this:

public class MyExplicitClass : ITest
{
    string ITest.TestMethod()
    {
        // Here I do things...
    }
}

As you can see, not much has changed between one implementation and the other.

The main difference is that we have removed the public accesor and we have inserted the interface’s name before the method. Though it can seem a trivial change, it’s not.

The method TestMethod is not a member of our class anymore. Now it implements directly a member of the ITest interface.

Differences between implicit and explicit implementation

When we explicitly implement a method, the method itself is not a member of the class. It belongs to the interface instead. This means that it’s not accessible by the other methods of the class, or from an instance of it, but only from an interface instance.

Said that, this is correct:

MyImplicitClass obj = new MyImplicitClass();
string a = obj.TestMethod();

but THIS IS NOT:

MyExplicitClass obj = new MyExplicitClass();
string a = obj.TestMethod();

In this last example, we’d get a compiler error, because TestMethod is not member of MyExplicitClass, but member of ITest. The right way to access this method should be this one:

ITest obj = new MyExplicitClass();
string a = obj.TestMethod();

Use Cases

Ok, so, the next question comes to mind… What’s it worth for?

Well, there are several answers to this question. Here I explain some, but I’m sure you can come with some more…

  • Disambiguation of methods with the same signature (name and parameters) between interfaces.
  • Hiding details of the interface that the class user doesn’t need to know.

Disambiguation of interfaces

Sometimes, two interfaces can have methods with the same signature, and we need different implementations for each of them.

For example:

interface IMovie { string GetTitle(); }
interface ISerie { string GetTitle(); }

public class Episode : IMovie, ISerie
{
    private string _title;
    private int _season;
    private int _episode;
    private string _episodeTitle;

    public Episode(string title, int season, int episode, string episodeTitle)
    {
        _title = title;
        _season = season;
        _episode = episode;
        _episodeTitle = episodeTitle;
    }

    // Implement IMovie
    string IMovie.GetTitle()
    {
        return _title;
    }

    // Implement ISerie
    string ISerie.GetTitle()
    {
        return string.Format("{0} - (S{1:00}/E{2:00}) {3}", 
              _title, _season, _episode, _episodeTitle);
    }

}

This implementation of Episode class allows us to get the general serie name (as a general title) if we treat it like a movie (IMovie). For this:

var serieObj = new Episode("The Walking Dead", 5, 10, "No Sanctuary");
Console.WriteLine(((IMovie)serieObj).GetTitle());

The result will be: The Walking Dead

But it also allows us to get the episode title with its season, chapter name and number if we treat it like an episode (ISerie). For this:

Console.WriteLine(((ISerie)serieObj).GetTitle());

The result will be: The Walking Dead – (S05/E10) No Sanctuary

Hiding the implementation

Another reason to explicitly implement interfaces is to conceal the implementation of the interface from the developers that will use that class. In this example, the interface IDisposable is needed for cleanup, but it isn’t meant to be called by the developer, but by a using block, so is a good practice to hide it from the members list.
In this case, we will only see the Dispose method when we work the class as an instance of IDisposable.

interface IDisposable
{
    void Dispose();
}

class MyFile : IDisposable
{
    void IDisposable.Dispose()
    {
        Close();
    }

    public void Close()
    {
        // Do what's necessary to close the file
        System.GC.SuppressFinalize(this);
    }
}

Example project

I’ve created a little project so you can see it working and make your tests. It’s published at my public BitBucket repository.

You can clone it with Git or VS at this uri:

https://bitbucket.org/joanvilarino/explicitinterfacedemo.git

You can also download the solution’s ZIP directly from my repository using this link to ZIP files.

Thank you for reading! See you all…

Follow me

Joan Vilariño

Senior .NET Developer at Ohpen
Developer for more than 25 years and IT, gadget, mobile and electronics enthusiast working and publishing from Barcelona.
Follow me