Morbid curiosity. Say I have this hierarchy:
class Base {
public virtual int Field { get; set; }
}
class Derived : Base {
int _field;
public override int Field { get { return _field; } set { _field = value; } }
}
If I instantiate Base
, the compiler will magically create a backing field for the Field
property.
So, given that Derived
does not reference the base implementation, does the backing field get created when Derived
is instantiated? Is this dictated in the C# spec, or left to compiler implementations?
Update
Turns out that the specification does indeed specifically state that automatically-implemented properties are implemented with a "hidden
backing field". (Sec 10.7.3) Nothing is said of my specific question. Assuming the word "hidden" there refers to the same member-hiding functionality provided by the new
keyword, I must conclude that the backing field is always created regardless of use.
I guess a related question might be "Is a backing field created for a auto-implemented property that is never accessed?" Same underlying argument, same conclusion.