Covariant return type of overriden methods

Topics: C# Language Design
Oct 15, 2014 at 12:32 AM
Edited Oct 15, 2014 at 4:11 AM
It would be nice to have covariant return types for methods. So the overriden method can return more specific type. Java has it from version 5.
 // Classes used as return types:
 
 class A {
 }
 
 class B : A {
 }
 
 // "Class B is more narrow than class A"
 // Classes demonstrating method overriding:
 
 class C {
     public virtual A GetFoo() {
         return new A();
     }
 }
 
 class D : C {
//Overriding GetFoo() in father class A
     public override B GetFoo() {
         return new B();
     }
 }
Developer
Oct 15, 2014 at 3:54 AM
hazzik wrote:
It would be nice to have covariant return types for methods. So the overriden method can return more specific type. Java has it from the beginning.
I agree, that would be nice.

Java did not have it from the beginning. I added that feature to Java for version 5.
Oct 15, 2014 at 4:11 AM
Edited Oct 15, 2014 at 4:18 AM
nmgafter wrote:
Java did not have it from the beginning
This does not really matter, but thanks, I've corrected the description
Oct 15, 2014 at 3:36 PM
Hi,

i think this could also help.

http://roslyn.codeplex.com/discussions/562656