7

I'm currently studying for my MS 70-515 exam. In one of the practices the author implements an interface both implicit as well as explicit. The explicit implementation just calls the implicit implementation. The explicit implementation is just listed without an explanation.

Does it make sense to have both an implicit and an explicit implementation of the interface? I would think the explicit implementation is redundant (in this case).

public class PassTextBox : TextBox, IScriptControl
{
    public virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
    {
        var descriptor = new ScriptControlDescriptor(
            "AjaxEnabled.PassTextBox", ClientID);
        // ...
        return new ScriptDescriptor[] {descriptor};
    }

    IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
    {
        return GetScriptDescriptors();
    }
}

BTW, the code seems to run just fine without the explicit implementation, as the implicit implementation is public.

It concerns MCTS Self-Paced Training Kit (Exam 70-515): Web Applications Development with Microsoft .NET Framework 4 Chapter 9, Lesson 2, Practice 3 to be precise.

comecme
  • 6,086
  • 10
  • 39
  • 67
  • possible duplicate of [Implicit and Explicit implementation of interface](http://stackoverflow.com/questions/2756520/implicit-and-explicit-implementation-of-interface) – M.Babcock Apr 15 '12 at 19:50
  • 2
    These exams always have an errata that's a mile long. You can submit your own: http://oreilly.com/catalog/errata.csp?isbn=9780735627406 – Hans Passant Apr 15 '12 at 20:03

1 Answers1

10

The explicit implementation seems to be totally superfluous.

I can't think of a way to call it where it would make a difference if you left it out.

There is one small difference, the implicit version is virtual meaning it could be overridden. The explicit version will always be called at this entry point. But since it only calls the virtual member that difference is not used here.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Marc Gravell [says](http://stackoverflow.com/a/2756728/537956) one reason to implement both could be _the non-explicit method is `virtual` or `abstract`, for subclasses to `override`_. In my example it is indeed `virtual`, but I don't see why that would be a reason to also implement the explicit interface. I'll ask Marc what he means by commenting on his answer. – comecme Apr 15 '12 at 19:48
  • You could have 2 completely different implementations. But whether that is desirable? And when exploiting that it would make more sense top put the implementation in explicit and have the virtual method call that. – H H Apr 15 '12 at 20:00
  • @Henk: if the virtual version would be overriden in a derived class, and an instance of that class would be called via an interface variable, which version would be called? Would the explicit version in the first class be called? And which version of the implicit implementation would be executed? The initial version or the overridden version? – comecme Apr 16 '12 at 06:19
  • 1
    When you call through an interface var it always goes to the explicit implementation. With an object var (base or derived) always to the implicit. – H H Apr 16 '12 at 08:28