|
|||||
|
Events and Delegates An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a simple mouse click, or it could be triggered by some other program logic. The object that raises (triggers) the event is called the event sender. The object that captures the event and responds to it is called the event receiver. In event communication, the event sender class does not know which object or method will receive (handle) the events it raises. Therefore we need an intermediary between the source and the receiver. The .NET Framework defines a special type (Delegate) that provides this functionality. In other words, events are abstractions for methods that broadcast certain application or system occurrences by invoking all the delegates that have registered themselves for that type of occurrence. The delegates are callback functions; the only difference is that a client must register the delegate with the event. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. Consuming Events To consume an event in an application, one must provide an event handler (an event-handling method) that executes program logic in response to the event and register the event handler with the event source. This process is referred to as event wiring.
|