简体   繁体   中英

explicit interface implementation, why explicit casting

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.

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, eg

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM