views:

226

answers:

1

I have following code:

public abstract class TestProperty
{
    public abstract Object PropertyValue { get; set; }
}

public class StringProperty: TestProperty
{
    public override string PropertyValue {get;set}
}

which generate compilation error, I wonder should I have to use generic type in TestProperty in order to achive my goal of having different type of the same name in the child class?

+6  A: 

Yes, C# doesn't support covariant return types. You can indeed use generics here:

public abstract class TestProperty<T>
{
    public abstract T PropertyValue { get; set; }
}

public class StringProperty : TestProperty<string>
{
}

There's no need to override at this point - and indeed unless you're going to do anything else, you might as well get rid of StringProperty entirely and just use TestProperty<string>.

Jon Skeet