6

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

(This example is taken from here: MSDN: Explicit Interface Implementation)

You have two interfaces as follows.

interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}

And you implement them explicitly.

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

Now, to use the interfaces you have the following code.

// Call the Paint methods from Main.

SampleClass obj = new SampleClass();
//obj.Paint();  // Compiler error.

IControl c = (IControl)obj;
c.Paint();  // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass. 

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

?

The reason for my confusion is because, for example, you can do the following

IDictionary<string, string> c = new Dictionary<string, string>();

without explicitly casting new Dictionary to IDictionary.

Thanks.

sakura-bloom
  • 4,524
  • 7
  • 46
  • 61
  • 1
    Because otherwise how would it know which implicit implementation to use? It could be either! – Liam Nov 06 '13 at 15:46
  • 3
    that should work - no need to cast. – Daniel A. White Nov 06 '13 at 15:46
  • @Liam, the left side is `IControl c`, isn't that enough? – sakura-bloom Nov 06 '13 at 15:47
  • The implicit conversion is fine. You'd only need the explicit cast if you're using implicitly typed local variables (var keyword); if you're using that in some places, that may lead to some confusion. – Allan Elder Nov 06 '13 at 15:55
  • 3
    It is confusing that MSDN uses the code as described in the question `IControl c = (IControl)obj;`. Why do they use a redundant explicit cast for an implicit conversion? It must be a careless mistake or exist for readability. – P.Brian.Mackey Nov 06 '13 at 15:59
  • @P.Brian.Mackey good question; I wonder if prior versions of the C# spec required it? – Allan Elder Nov 06 '13 at 16:08

2 Answers2

17

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method?

The member effectively doesn't exist on the class, as far as the compiler's concerned - it only exists on the interface. You don't have to explicitly cast though - you just have to have a reference which has a compile-time type of the interface. That can be done however you like, including implicit conversions.

In the above code block, why do you have

IControl c = (IControl)obj;

as opposed to

IControl c = obj;

You don't have to. The implicit conversion should be absolutely fine. You would have to cast explicitly in order to call the method in a single expression, e.g.

obj.Paint(); // Invalid
((IControl) obj).Paint(); // Valid

But if you go through an implicit conversion via assignment to a separate local variable of the interface type, that's fine - the method is still being called with a target expression which is of the interface type.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So you're saying that if I had an object `MyClass o = new MyClass();` that implemented an interface explicitly I couldn't do `o.InterfaceMethod()`; but if it were `IInterface o = new MyClass();` that would work. Do I have that right? – Mike Perrenoud Nov 06 '13 at 15:49
  • 1
    It's worth noting here that in C# you can control implicit and explicit casting via the implict (http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.110).aspx) and explicit (http://msdn.microsoft.com/en-us/library/xhbhezf4(v=vs.110).aspx) operator overloads – Dweeberly Nov 06 '13 at 16:00
2

Explicit Interface Implementation is required only when a type inherits from multiple interfaces and some of the methods have same name/signature in more than one interfaces.

Rest it is matter of preference, and convention.

mpleClass obj = new SampleClass();
//obj.Paint();  // Compiler error. 

obj.Paint() --> This is error as When Explicit Interface implementation is done, the underlying interface implementation requires explicit cast as specified in MSDN

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • 1
    That seems to answer a different question - namely what the *purpose* of explicit interface implementation is. It doesn't answer the question actually being asked. – Jon Skeet Nov 06 '13 at 15:48