This documentation is archived and is not being maintained.

Raising Multiple Events

If your class raises multiple events and you program these as described in Raising an Event, the compiler generates one field per event delegate instance. If the number of events is large, the storage cost of one field per delegate may not be acceptable. For those situations, the .NET Framework provides a construct called event properties that you can use together with another data structure (of your choice) to store event delegates.

Event properties consist of event declarations accompanied by event accessors, as shown in the following example. Event accessors are methods you define to allow event delegate instances to be added or removed from the storage data structure. Note that event properties are slower than event fields, as each event delegate has to be retrieved before it can be invoked. The trade-off is between memory and speed. If your class defines many events that are infrequently raised, you will want to implement event properties. Windows Forms controls and ASP.NET server controls use event properties instead of event fields.

The following C# example shows how to use event properties to implement events. The keywords of the event property construct are in bold type.

Note   Event properties are not supported in Visual Basic .NET in this release.
// The class Control defines two events-MouseUp and MouseDown-that 
// use event properties.
class Control: Component {
   // omit start
   Delegate _handler = null;
   // omit end
   // Defines a unique key for each event.
   static readonly object mouseDownEventKey = new object();
   static readonly object mouseUpEventKey = new object();
   // Defines a method to retrieve the event delegate associated with key.
   protected Delegate GetEventHandler(object key) {...}
   // Defines the helper method for the add accessor.
   protected void AddEventHandler(object key, Delegate handler) {...}
   // Defines the helper method for the remove accessor.
   protected void RemoveEventHandler(object key, Delegate handler) {...}
   // Defines the MouseDown event property.
   public event MouseEventHandler MouseDown {  
      // The add event accessor method.
      add { AddEventHandler(mouseDownEventKey, value); }
      // The remove event accessor method.
      remove { RemoveEventHandler(mouseDownEventKey, value); }
   // Defines the MouseUp event property.
   public event MouseEventHandler MouseUp {
      // The add event accessor method.
      add { AddEventHandler(mouseUpEventKey, value); }
      // The remove event accessor method.
      remove { RemoveEventHandler(mouseUpEventKey, value); }
   // Define a private data structure to store the 
   // event delegates.
   // To do - Define your data stucture;

The .NET Framework provides a data structure for storing event delegates, the System.ComponentModel.EventHandlerList class, which is used by classes in the Framework that raise multiple events. You can use this class or define your own data structure for storage.

See Also

Handling and Raising Events