13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Classes}public event D Ev {add {lock(this) { __Ev = __Ev + value; }}remove {lock(this) { __Ev = __Ev - value; }}}Within the class X, references to Ev are compiled to reference the hidden field __Ev instead. The name “__Ev” isarbitrary; the hidden field could have any name or no name at all.Similarly, a static event declaration of the form: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}end note]public static event D Ev {add {lock(typeof(X)) { __Ev = __Ev + value; }}remove {lock(typeof(X)) { __Ev = __Ev - value; }}}17.7.2 Event accessors[Note: Event declarations typically omit event-accessor-declarations, as in the Button example above. Onesituation for doing so involves the case in which the storage cost of one field per event is not acceptable. In suchcases, a class can include event-accessor-declarations and use a private mechanism for storing the list of eventhandlers. Similarly, in cases where the handling of an event requires access to external resources, event accessorsmay be used to manage these resources. end note]The event-accessor-declarations of an event specify the executable statements associated with adding andremoving event handlers.The accessor declarations consist of an add-accessor-declaration and a remove-accessor-declaration. Eachaccessor declaration consists of the token add or remove followed by a block. The block associated with an addaccessor-declarationspecifies the statements to execute when an event handler is added, and the block associatedwith 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 valueparameter of the event type, and a void return type. The implicit parameter of an event accessor is namedvalue. When an event is used in an event assignment, the appropriate event accessor is used. Specifically, if theassignment operator is += then the add accessor is used, and if the assignment operator is –= then the removeaccessor is used. In either case, the right-hand operand of the assignment operator is used as the argument to theevent accessor. The block of an add-accessor-declaration or a remove-accessor-declaration must conform to therules for void methods described in §17.5.8. In particular, return statements in such a block are not permittedto specify an expression.Since an event accessor implicitly has a parameter named value, it is a compile-time error for a local variable orconstant declared in an event accessor to have that name.249

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!