简体   繁体   English

返回接口实现时的C#编译器错误

[英]C# Compiler Error on Returning Interface Implementation

I'm trying to understand why the code below does not compile. 我试图理解为什么下面的代码不能编译。 The error I get is: 我得到的错误是:

'MyClass' does not implement interface member 'IFace.Deliver()'. 'MyClass'没有实现接口成员'IFace.Deliver()'。 'MyClass.Deliver()' cannot implement 'IFace.Deliver()' because it does not have the matching return type of 'IDoSomething'. 'MyClass.Deliver()'无法实现'IFace.Deliver()',因为它没有匹配的返回类型'IDoSomething'。

Please note that I am not asking for someone to re-word the compiler error and and simply tell me the same thing as the compiler, or to say, "...the compiler doesn't support it", nor am I asking how to "fix" the code to make it compile (I can do that by uncommenting the method shown in the example). 请注意,我并不是要求某人重新编写编译器错误并简单地告诉我与编译器相同的事情,或者说“......编译器不支持它”,我也不会问“修复”代码以使其编译(我可以通过取消注释示例中显示的方法来实现)。

I'm trying to understand the rationale in the code not compiling. 我试图理解代码中没有编译的基本原理。 If someone could point me to a location in the C# specification that addresses this issue, that would be perfect. 如果有人能指出我在C#规范中解决此问题的位置,那将是完美的。

To my way of thinking, the code should be valid because: 以我的思维方式,代码应该是有效的,因为:

  1. It's logically valid. 它在逻辑上是有效的。 A ThisOrThat object IS a proper implementation of IDoSomething (unless there's some subtle issue I'm missing). ThisOrThat对象是IDoSomething的正确实现(除非我遗漏了一些微妙的问题)。
  2. It is consistent with the spirit of polymorphism. 它符合多态性的精神。 If a method is declared as returning an interface, it must return an instance of a concrete class which implements the interface. 如果方法声明为返回接口,则它必须返回实现接口的具体类的实例。 Why should it be invalid then for the same method to be declared as returning a concrete class that implements the interface? 为什么它应该是无效的,因为同一个方法被声明为返回一个实现接口的具体类? The compiler can certainly verify the class' implementation. 编译器当然可以验证类的实现。 (And yes, I already know the compiler does not approve.) (是的,我已经知道编译器不会批准。)

Admittedly, #2 is a rather vague reason and I don't want to get into a philosophical discussion. 不可否认,#2是一个相当模糊的原因,我不想进入哲学讨论。 I'm really looking for a technical reason why it doesn't work. 我真的在找技术原因,为什么它不起作用。

As I said, I know what I need to do to "fix" it but I really want to understand WHY it should not compile as written. 正如我所说的,我知道我需要做些什么来“修复”它,但我真的想要理解为什么它不应该像编写的那样编译。

  interface IFace {
     IDoSomething Deliver();
  }

  interface IDoSomething {
  }


  class ThisOrThat : IDoSomething {
  }


  class MyClass : IFace {
     public ThisOrThat Deliver() {
        return new ThisOrThat();
     }

     // Uncomment the lines below to make it compile.
     //IDoSomething IFace.Deliver() {
     //   return Deliver();
     //}
  }

The problem is 'MyClass' doesn't implement the interface because the return type of 'Deliver()' is different than the return type of 'Deliver()' in the interface. 问题是'MyClass'没有实现接口,因为'Deliver()'的返回类型与接口中的'Deliver()'的返回类型不同。 You can argue that it is technically fulfilling the requirements of the interface because of polymorphism, but that doesn't mean the compiler has to support the feature. 您可以争辩说,由于多态性,它在技术上满足了接口的要求,但这并不意味着编译器必须支持该功能。

What problem are you trying to solve? 你想解决什么问题? Why can't 'Deliver' in 'MyClass' just return an instance of 'IDoSomething'? 为什么“MyClass”中的'Deliver'不能只返回'IDoSomething'的实例? Or just use overloading like you did above. 或者像上面一样使用重载。 I'm not sure why you need this feature. 我不确定你为什么需要这个功能。 I think this feature is not supported by the C# because there is no need for it. 我认为C#不支持此功能,因为不需要它。

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

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