Say I have the following code:
class Parent
{
static string MyField = "ParentField";
public virtual string DoSomething()
{
return MyField;
}
}
class Child : Parent
{
static new string MyField = "ChildField";
}
Now I want to be able to do both of the following:
Console.WriteLine(Parent.MyField);
Console.WriteLine(Child.MyField);
These work as expected, but I would also like to do this:
Child c = new Child();
Console.WriteLine(c.DoSomething());
Since DoSomething() is not defined for the Child class, it's the Parent's MyField that gets returned, but what I want is the Child's MyField.
So my question is: Is there any way I can do this?
NOTE: Overriding the method in the Child class is an option, but since I will have lots of Child classes inheriting from the Parent and the method is supposed to do the same in all of them, changing something in this method would bring a lot of trouble.