Let's 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 one of the following:
Console.WriteLine(Parent.MyField); Console.WriteLine(Child.MyField);
They 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, the parent MyField is returned, but I want the Child MyField to be.
So my question is: is there a way to do this?
NOTE. Overriding a method in the Child class is an option, but since I will have many child classes inheriting from the parent, and the method should do the same in all of them, changing something in this method will lead to a lot of trouble.
source share