Reputation: 2208
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.Deliver()' cannot implement 'IFace.Deliver()' because it does not have the matching return type of '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.
To my way of thinking, the code should be valid because:
Admittedly, #2 is a rather vague reason and I don't want to get into a philosophical discussion. 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();
//}
}
Upvotes: 0
Views: 228
Reputation: 1006
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. 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'? 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.
Upvotes: 2