This is the scenario:
class BaseClass
{
public virtual string Prop {get; set;}
}
class ChildClass : BaseClass
{
public override string Prop {get; set;}
}
//program
...
ChildClass instance = new ChildClass;
Console.WriteLine(instance.Prop); //accessing ChildClass.Prop
...
The question is how to access BaseClass.Prop in a instance of ChildClass? Will casting do the trick?
Console.WriteLine((instance as BaseClass).Prop); //accessing BaseClass.Prop
-EDIT-
A lot of people suggested casting. In C++ it would not work because polymorphism would still ensure that the child property is called. Isn’t that the case in C#?
In C++ you would solve the issue by doing:
instance.(BaseClass::get_Prop())
The semantics of overriding a property won’t let you access the base of an overridden property. And this makes sense. Classes that use ChildClass shouldn’t care if the property is overridden or not. When the property is used by another class it’s up to ChildClass to return the appropriate value.
Usually questions like these indicate an attempt to solve a different problem. What kind of problems are you facing that you need direct access to the base property?