X hits on this document





250 / 396

Chapter ‎18   Unsafe code

Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors behave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in §‎10.5.3, §‎10.5.4, §‎10.5.5, and §‎10.5.6 apply as if accessors were methods of a corresponding form:

A get accessor corresponds to a parameterless method with a return value of the property type and the same modifiers as the containing property.

A set accessor corresponds to a method with a single value parameter of the property type, a void return type, and the same modifiers as the containing property.

In the example

abstract class A { int y;

public virtual int X { get { return 0; } }

public virtual int Y { get { return y; } set { y = value; } }

public abstract int Z { get; set; } }

X is a virtual read-only property, Y is a virtual read-write property, and Z is an abstract read-write property. Because Z is abstract, the containing class A must also be declared abstract.

A class that derives from A is show below:

class B: A { int z;

public override int X { get { return base.X + 1; } }

public override int Y { set { base.Y = value < 0? 0: value; } }

public override int Z { get { return z; } set { z = value; } } }

Here, the declarations of X, Y, and Z are overriding property declarations. Each property declaration exactly matches the accessibility 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.

10.7 Events

An event is a member that enables an object or class to provide notifications. Clients can attach executable code for events by supplying event handlers.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.237

Document info
Document views1374
Page views1374
Page last viewedSun Jan 22 14:27:43 UTC 2017