X hits on this document





254 / 396

Chapter ‎18   Unsafe code

class X { public static event D Ev; }

could be compiled to something equivalent to:

class X { private static D __Ev;  // field to hold the delegate

public static event D Ev { add { lock(typeof(X)) { __Ev = __Ev + value; } }

remove { lock(typeof(X)) { __Ev = __Ev - value; } } } }

10.7.2 Event accessors

Event declarations typically omit event-accessor-declarations, as in the Button example above. One situation for doing so involves the case in which the storage cost of one field per event is not acceptable. In such cases, a class can include event-accessor-declarations and use a private mechanism for storing the list of event handlers.

The event-accessor-declarations of an event specify the executable statements associated with adding and removing event handlers.

The accessor declarations consist of an add-accessor-declaration and a remove-accessor-declaration. Each accessor declaration consists of the token add or remove followed by a block. The block associated with an add-accessor-declaration specifies the statements to execute when an event handler is added, and the block associated with a remove-accessor-declaration specifies the statements to execute when an event handler is removed.

Each add-accessor-declaration and remove-accessor-declaration corresponds to a method with a single value parameter of the event type and a void return type. The implicit parameter of an event accessor is named value. When an event is used in an event assignment, the appropriate event accessor is used. Specifically, if the assignment operator is += then the add accessor is used, and if the assignment operator is -= then the remove accessor is used. In either case, the right-hand operand of the assignment operator is used as the argument to the event accessor. The block of an add-accessor-declaration or a remove-accessor-declaration must conform to the rules for void methods described in §‎10.5.8. In particular, return statements in such a block are not permitted to specify an expression.

Since an event accessor implicitly has a parameter named value, it is a compile-time error for a local variable or constant declared in an event accessor to have that name.

In the example

class Control: Component { // Unique keys for events static readonly object mouseDownEventKey = new object(); static readonly object mouseUpEventKey = new object();

// Return event handler associated with key protected Delegate GetEventHandler(object key) {...}

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

Document info
Document views354
Page views354
Page last viewedThu Oct 20 20:32:08 UTC 2016