Luis Filipe Luis Filipe - 16 days ago 6
C# Question

Can I Override with derived types?

As far as i know it is not possible to do the following in C# 2.0

public class Father
{
public virtual Father SomePropertyName
{
get
{
return this;
}
}
}

public class Child : Father
{
public override Child SomePropertyName
{
get
{
return this;
}
}
}


I workaround the problem by creating the property in the derived class as "new", but of course that is not polymorphic.

public new Child SomePropertyName


Is there any solution in 2.0?
What about any features in 3.5 that address this matter?

Answer

EDIT: I had just woken up when I wrote the original answer, and I think I made it a little less than clear, and used the wrong word a couple times. Below is a modified version that says the same thing, but (I hope) explains better, and uses the correct terminology.

This is not possible in any .NET language because of type-safety concerns. In type-safe languages, you must provide covariance for return values, and contravariance for parameters. Take this code:

class B {
    S Get();
    Set(S);
}
class D : B {
    T Get();
    Set(T);
}

For the Get methods, covariance means that T must either be S or a type derived from S. Otherwise, if you had a reference to an object of type D stored in a variable typed B, when you called B.Get() you wouldn't get an object representable as an S back -- breaking the type system.

For the Set methods, contravariance means that T must either be S or a type that S derives from. Otherwise, if you had a reference to an object of type D stored in a variable typed B, when you called B.Set(X), where X was of type S but not of type T, D::Set(T) would get an object of a type it did not expect.

In C#, there was a conscious decision to disallow changing the type when overloading properties, even when they have only one of the getter/setter pair, because it would otherwise have very inconsistent behavior ("You mean, I can change the type on the one with a getter, but not one with both a getter and setter? Why not?!?" -- Anonymous Alternate Universe Newbie).