简体   繁体   English

显式接口实现,为什么显式转换

[英]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 ) (此示例取自此处: MSDN:显式接口实现

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 . 没有明确地将new DictionaryIDictionary

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 obj.Paint() - >这是错误,因为当完成显式接口实现时,底层接口实现需要MSDN中指定的显式obj.Paint()

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. 显式接口成员实现只能通过接口实例访问,并且在这种情况下仅通过其成员名称引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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