Given the following hypothetical situation:
class ClassParent { } interface IClassProvider { ClassParent Get(); }
Why is it an illegal implementation of IClassProvider:
class ClassChild : ClassParent, IClassProvider { ClassChild Get() { return this; } }
It also does not work for inheriting properties and implementing a base class instead of an interface, as in the example.
ClassChild - ClassParent. Why doesn't it then compile? The compiler clearly knows the type of compilation time of the class, so the following works:
void DoSomething(object o) { ... }; void DoSomething(ConcreteClass c) { ... }; DoSomething(new ConcreteClass());
In multi-level scenarios, this makes me have a bunch of proxy methods and it is useless to clutter up my code when the underlying situation is clear. Honestly, I cannot come up with any problems or ambiguities if this is supported. I would be pleased with the compile-time resolution (static), since it works with overloads in the second example.
Edit: I know that the compiler expects the following:
class ClassChild : ClassParent, IClassProvider { ClassParent Get() { return this; } }
and I know that this will work, I ask you to explain why it is not supported, or the scenario in which it will lead to a) problems or b) ambiguities is worse than overloads that are supported.
Edit 2: This seems to be another duplicate of the same old question, which the MS apparently answered in this SO question . I mark the @Euphoric post as an answer because it provided a name for the function that helped find the “solution”.