Asked • 07/19/19

Partially Overriding a Virtual Auto-Property in a Child Class?

Time for a theoretical question I just ran across.The following code is valid and compiles: public class Parent { public virtual object TestProperty { get; set; } } public class Child : Parent { private string _testValue = "Hello World!"; public override object TestProperty { get { return _testValue; } } } public class Consumer { Parent p = new Child(); public Consumer(){ p.TestProperty = 3; } }My question is: **Why does C# allow me to partially override the `TestProperty` auto property in a child when it leads to partially unpredictable behavior?** Is there a practical application?I'm allowed to set the value of TestProperty using the parent's setter (I checked the IL being generated and the setter is still setting the backing object in the parent class) even though value is not accessible to the public.

1 Expert Answer


Caleb D. answered • 07/27/19

New to Wyzant

Simple Explanations

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.