- Multicast delegates are used extensively in event handling. Event source objects send event notifications to recipient objects that have registered to receive that event. To
register for an event, the recipient creates a method designed to handle the event, then creates a delegate for that method and passes the delegate to the event source. The
source calls the delegate when the event occurs. The delegate then calls the event handling method on the recipient, delivering the event data. The
delegate type for a given event is defined by the event source.
- Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisherand the classes that receive (or handle) the event are called subscribers. In a typical C# Windows Forms or Web application, you subscribe to events raised by controls such as buttons and list boxes.
- Events have the following properties:
- The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event.
- An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.
- Events that have no subscribers are never raised.
- Events are typically used to signal user actions such as button clicks or menu selections in graphical user interfaces.
- When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised.
- In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class.
- An event is a special kind of multicast delegate that can only be invoked from within the class that it is declared in. By convention, event delegates in the .NET Framework have two parameters, the source that raised the event and the data for the event.
- By convention in the .NET Framework, when an event is raised, it passes event data to its event handlers. The event data is provided by theSystem.EventArgs class or by a class that is derived from it. Often, an event has no custom data; the fact that the event was fired provides all of the information that event handlers require. In this case, the event can pass an EventArgs object to its handlers. The EventArgs class has only a single member, Empty, that is not inherited from System.Object. It can be used to instantiate a new EventArgs class. If an event does have custom data, it can pass an instance of a class derived from EventArgs to event handlers. Depending on the precise data the event passes to handlers, you may be able to use an existing event data class in the .NET Framework. For example, if your event handler allows the action associated with the event to be cancelled, you can use the CancelEventArgs class. When you need to provide custom data to handlers and a existing class is not available, you can define your own event data class. It must derive fromSystem.EventArgs. Byconvention, this class is named EventNameEventArgs.
- The .NET Framework allows subscribers to register for event notifications either statically or dynamically. Static event handlers are in effect for the entire life of the class whose events they handle. This is the most common method of handling events. Dynamic event handlers are explicitly activated and deactivated during program execution, usually in response to some conditional program logic. For example, they can be used if event notifications are needed only under certain conditions or if an application provides multiple event handlers and run-time conditions define the appropriate one to use.
- If you want your class to raise an event, you must provide the following three elements:
- A class that provides event data.
- An event delegate.
- The class that raises the event.