69

In Visual Studio I can right-click on an interface and choose to Implement Interface, or Implement Interface Explicitly.

Visual Studio Screenshot

public class Test : ITest
{
    public string Id // Generated by Implement Interface
    {
        get { throw new NotImplementedException(); }
    }

    string ITest.Id // Generated by Implement Interface Explicitly
    {
        get { throw new NotImplementedException(); }
    }
}

The only difference I see between the two is that the Interface name is added to the interface properties and methods when they're created if you choose to Implement Interface Explicitly.

I find it makes the code a bit more readable since I can see where that method/property comes from, however does this make any difference in how the class is used or compiled? And does it really matter if I implement my interfaces implicitly or explicitly?

5 Answers 5

56

Check out the top answer from Andrew Barrett for "implicit vs explicit interface implementation" on SO.

Basically:

  • Implicit: you access the interface methods and properties as if they were part of the class.
  • Explicit: you can only access methods and properties when treating the class as the implemented interface.

Code examples:

Implicit:

Test t = new Test();
t.Id; // OK
((ITest)t).Id; // OK

Explicit:

Test t = new Test();
t.Id; // Not OK
((ITest)t).Id; // OK

In terms of "when" you have to implement an interface explicitly, it is when your class already has a method with the same signature as one of the methods of your interface, or when your class implements several interfaces that share methods with the same signatures but incompatible contracts.

3
  • 1
    I also found explicit implementation useful to have a sort of "hidden" interface with unsafe operations. It also makes calls to those methods stand out more which is a good thing for unsafe things. Feb 22, 2012 at 15:46
  • Its also worth mentioning that there is a performance cost to using explicit interfaces, since it needs to box/unbox the object anytime you reference the property/method. Because of this, it's better to use implicit interfaces if possible
    – Rachel
    Feb 22, 2012 at 15:53
  • 4
    @Rachel: As far as I'm aware that performance cost only applies to value types.
    – Groky
    Feb 22, 2012 at 18:27
9

There is also a difference in how you call the method.

When using an explicit interface implementation, you must use the interface type in order to call that specific implementation.

So, in calling code you would need to use a variable of type ITest in order to access ITest.Id.

The article Explicit Interface Implementation (C# Programming Guide) on MSDN has a good example.

5

Based on Jalayan's answer,

  • Implicit: you access the interface methods and properties as if they were part of the class.
  • Explicit: you can only access methods and properties when treating the class as the implemented interface.

enter image description here

4

It allows you to implement two interfaces that define the same method. However, if you explicitly implement the interface, the methods can only be accessed when the variable is typed to that explicit interface.

See: Explicit Interface Implementation Tutorial

4

EDIT: It shouldn't make a difference You shouldn't do it unless your class implements two interfaces with the same properties, as you'll have to cast to the relevant interface before you can access the member:

public interface ITest
{
    string Id { get; }
}

public interface IAlsoTest
{
    string Id { get; }
}

public interface ITestToo
{
    int Id { get; }
}

public class Test : ITest, IAlsoTest
{
    // Valid implicit implementation of BOTH interfaces
    public string Id
    {
        get { throw new NotImplementedException(); }
    }
}

public class TestSeparately : ITest, ITestToo
{
    // This way we can do different things depending
    // on which interface the callee called from.
    string ITest.Id
    {
        get { throw new NotImplementedException(); }
    }

    int ITestToo.Id
    {
        get { throw new NotImplementedException(); }
    }
}

public class TestOuch
{
    public void DoStuff()
    {
        var ts = new TestSeparately();

        // Works
        Console.WriteLine(((ITest)ts).Id);

        // Works
        Console.WriteLine(((ITestToo)ts).Id);

        // Not valid! Which one did we want to call?
        Console.WriteLine(ts.Id);
    }
}

The example usage holds when you explicitly implement an interface member even if you are only using a single interface (which I always forget :S), so I would try and avoid the explicit implementation whenever possible, as it will hide class members if they're not cast to the right interface (which is fairly confusing).

Not the answer you're looking for? Browse other questions tagged or ask your own question.