Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.7 Eventsprevious at this levelnext at this level 17.7.1 Field-like events Paragraph 11 Within the program text of the class or struct that contains the declaration of an event, certain events can be used like fields. 2 To be used in this way, an event must not be abstract or extern, and must not explicitly include event-accessor-declarations. 3 Such an event can be used in any context that permits a field. 4 The field contains a delegate (§22), which refers to the list of event handlers that have been added to the event. 5 If no event handlers have been added, the field contains null. [Example: In the example
public delegate void EventHandler(object sender, EventArgs e);  
public class Button: Control  
{  
   public event EventHandler Click;  
   protected void OnClick(EventArgs e) {  
      if (Click != null) Click(this, e);  
   }  
   public void Reset() {  
      Click = null;  
   }  
}  
Click is used as a field within the Button class. As the example demonstrates, the field can be examined, modified, and used in delegate invocation expressions. The OnClick method in the Button class "raises" the Click event. The notion of raising an event is precisely equivalent to invoking the delegate represented by the event-thus, there are no special language constructs for raising events. Note that the delegate invocation is preceded by a check that ensures the delegate is non-null. Outside the declaration of the Button class, the Clickmember can only be used on the left-hand side of the += and -= operators, as in
b.Click += new EventHandler(...);  
which appends a delegate to the invocation list of the Click event, and
b.Click -= new EventHandler(...);  
which removes a delegate from the invocation list of the Click event. end example]
Paragraph 21 When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and creates accessors for the event that add or remove event handlers to the delegate field. 2 In order to be thread-safe, the addition or removal operations are done while holding the lock (§15.12) on the containing object for an instance event, or the type object (§14.5.11) for a static event. [Note: Thus, an instance event declaration of the form:
class X {  
   public event D Ev;  
}  
could be compiled to something equivalent to:
class X {  
   private D __Ev;  // field to hold the delegate  
   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" is arbitrary; 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  
   public static event D Ev {  
      add {  
         lock(typeof(X)) { __Ev = __Ev + value; }  
      }  
      remove {  
         lock(typeof(X)) { __Ev = __Ev - value; }  
      }  
   }  
}  
end note]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS