Provided a property is not static
, a property declaration may include a virtual
modifier or an abstract
modifier on either or both of its accessors. There is no requirement that the modifiers be the same for each accessor. For example, it is possible for a property to have a non-virtual get
accessor and a virtual set
accessor.
The virtual accessors of an inherited property can be overridden in a derived class by including a property declaration that specifies override
directives on its accessors. This is known as an overriding property declaration. An overriding property declaration does not declare a new property. Instead, it simply specializes the implementations of the virtual accessors of an existing property.
It is an error to mix override and non-override accessors in a property declaration. If a property declaration includes both accessors, then both must include an override
directive or both must omit it.
An overriding property declaration must specify the exact same access modifiers, type, and name as the inherited property, and it can override only those inherited accessors that are virtual. For example, if an inherited property has a non-virtual get
accessor and a virtual set
accessor, then an overriding property declaration can only include an override
set
accessor.
When both accessors of an inherited property are virtual, an overriding property declaration is permitted to only override one of the accessors.
Except for differences in declaration and invocation syntax, virtual, override, and abstract accessors behave exactly like a virtual, override and abstract methods. Specifically, the rules described in §10.5.3, §10.5.4, and §10.5.5 apply as if accessors were methods of a corresponding form:
get
accessor corresponds to a parameterless method with a return value of the property type and a set of modifiers formed by combining the modifiers of the property and the modifier of the accessor.set
accessor corresponds to a method with a single value parameter of the property type, a void
return type, and a set of modifiers formed by combining the modifiers of the property and the modifier of the accessor.In the example
abstract class A { int y; public int X { virtual get { return 0; } } public int Y { get { return y; } virtual set { y = value; } } protected int Z { abstract get; abstract set; } }
X
is a read-only property with a virtual get
accessor, Y
is a read-write property with a non-virtual get
accessor and a virtual set
accessor, and Z
is a read-write property with abstract get
and set
accessors. Because the containing class is abstract, Z
is permitted to have abstract accessors.
A class that derives from A
is shown below:
class B: A { int z; public int X { override get { return base.X + 1; } } public int Y { override set { base.Y = value < 0? 0: value; } } protected int Z { override get { return z; } override set { z = value; } } }
Here, because their accessors specify the override
modifier, the declarations of X
, Y
, and Z
are overriding property declarations. Each property declaration exactly matches the access modifiers, type, and name of the corresponding inherited property. The get
accessor of X
and the set
accessor of Y
use the base
keyword to access the inherited accessors. The declaration of Z
overrides both abstract accessors—thus, there are no outstanding abstract function members in B
, and B
is permitted to be a non-abstract class.