c# - Partially Overriding a Virtual Auto-Property in a Child Class -
time theoretical question ran across.
the following code valid , compiles:
public class parent { public virtual object testproperty { get; set; } } public class child : parent { private string _testvalue = "hello world!"; public override object testproperty { { return _testvalue; } } } public class consumer { parent p = new child(); public consumer(){ p.testproperty = 3; } }
my question is:
why c# allow me partially override testproperty
auto property in child when leads partially unpredictable behavior? there practical application?
i'm allowed set value of testproperty using parent's setter (i checked il being generated , setter still setting backing object in parent class) though value not accessible public.
this behavior consistent non-auto-implemented properties in c#. it's been possible override or set method virtual property. hence making impossible auto-implemented property create unnecessary inconsistency.
for example, following legal
class { public virtual int p1 { { return 42; } set { } } } class b : { public override int p1 { { return 18; } } }
Comments
Post a Comment