Events in the .NET Framework are based on the delegate model. The delegate model follows the observer design pattern, which enables a subscriber to register with, and receive notifications from, a provider. An event sender pushes a notification that an event has happened, and an event receiver receives that notification and defines a response to it.
An event is a message sent by an object to signal the occurrence of an action. The object that raises the event is called the event sender . The event sender doesn't know which object or method will receive (handle) the events it raises. The event is typically a member of the event sender. The .NET Framework follows a naming pattern of ending all event data classes with EventArgs.
Example of standard .Net framework event delegate:
A delegate is a type that holds a reference to a method. A delegate is declared with a signature that shows the return type and parameters for the methods it references, and can hold references only to methods that match its signature. A delegate is thus equivalent to a function pointer or a callback in Delphi. The Runtime Library provides the following delegates or function pointers to support the c# standard delegates such as the one above.
The following example shows an event handler method named c_ThresholdReached that matches the signature for the TClrEventHandler function pointer. The method subscribes to the ThresholdReached event.
Title |
Description |
---|---|
Contains examples of raising and consuming events. |