Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 16 down vote accepted

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.

share|improve this answer
    
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. –  Tamás Szelei Feb 22 '12 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 '12 at 15:53
    
@Rachel: As far as I'm aware that performance cost only applies to value types. –  Groky Feb 22 '12 at 18:27
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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