Public class ClassB : ClassA
{
}
public class GeneralClass
{
public ClassA test
{
get{}
set{}
}
}
public class specificClass :GeneralClass
{
public ClassB test
{
get{}
set{}
}
}
As you can see the property test is inherited in specificClass from generalClass but here I want to change the type to ClassB (ClassB:ClassA) . can I do something like this so anytime I’m using the specializedClass.test I don’t need to typecast it to classB ?
edit 1 : I forgot to mention that now it’s giving me the warning that test hide inherited member test .use new keyword if hiding is intended .
You can’t modify the return type as you override methods and properties. C# doesn’t support return type covariance.
By the way, if you don’t need to override the property and just want to return the base value casted to some specific type, you can declare a
newproperty to hide the base class one. Note that it’s not return type covariance. It’s just a distinct property that happens to have an identical name: