Say I have the following code:
class Parent
{
static string MyField = \"ParentField\";
public virtual string DoSomething()
{
return MyField
Yes, override DoSomething:
class Child
{
static new string MyField = "ChildField";
public virtual string DoSomething()
{
return MyField;
}
}
Use a static property instead of a static variable. Then you can override the property in the child class instead of creating a "new" field.
The only way is overriding DoSomething
method or else it is not possible. The DoSomething
in Parent
method essentially translates to:
public virtual string DoSomething()
{
return Parent.MyField;
}
When you "new
" a property, it only applies to that type - in this case Child
class. If the property is accessed via the 'Parent', it will always return the original property.