Extending abstract classes in c#

Posted by ng on Stack Overflow See other posts from Stack Overflow or by ng
Published on 2010-05-05T08:47:50Z Indexed on 2010/05/05 9:38 UTC
Read the original article Hit count: 139

Filed under:
|

I am a Java developer and I have noticed some differences in extending abstract classes in c# as opposed to Java. I was wondering how a c# developer would achived the following.

1) Covarience

public abstract class A {
   public abstract List<B> List();
}
public class BList : List<T> where T : B {
}
public abstract class C : A { 
   public abstract BList List();
}

So in the above hierarchy, there is covarience in C where it returns a type compatible with what A returns. However this gives me an error in Visual Studio. Is there a way to specify a covarient return type in c#?

2) Adding a setter to a property

public abstract class A {
   public abstract String Name { get; }
}
public abstract class B : A {
   public abstract String Name { get; set }
}

Here the compiler complains of hiding. Any suggestions?

Please do not suggest using interfaces unless that is the ONLY way to do this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about java