.NET Development Foundation/Using events
Jump to navigation Jump to search
|.NET Development Foundation|
System types and collections: Using events and delegates
Using events and delegates
Exam objectives: Control interactions between .NET Framework application components by using events and delegates.
(Refer System namespace)
Delegate class - MSDN
- Delegates hold pointers to one or more functions and invoke them as needed.
- One common use of delegates is for event handling. A class that raises an event does not know what objects or methods want to receive the event, so an intermediary or pointer mechanism is needed between the object raising the event and the object(s) receiving the event. Delegates can be used as function pointers to accomplish this.
- A delegate is a class, but unlike a regular class it has a signature. In the .Net framework you just declare the delegate and the CLR handles the implementation of the class.
//delegate declaration public delegate void AlarmEventHandler(object sender,EventArgs e);
- The first complete example just declare a delegate type, then declare a variable of that type, assign a functon to it and execute the delegate variable which has the effect of executing the function.
- The second delegate example implement the concept of a callback function. A function is called with a delegate as an argument. When it executes the delegate it has no knowledge of exactly what function is executed. Part of its behavior is delegated to the function passed as a parameter (thru the delegate).
- The third delegate example uses a delegate member to produce the same pattern as an event. Note that executing the delegate member without assigning at least one function will cause an exception. Also assigning 2 functions with the += operator will execute the 2 functions when the delegate is executed. If the delegate has a return code, the return value of the last executed function will be returned.
- The fourth example is the basic example of an event. It is exactly as the third example with the event keyword added to the member declaration and a test before calling the event because of the exception thrown on an "empty" event. This example shows clearly that an event is nothing more then a multicast delegate.
- Executing the functions associated with an event is called raising the event.
- The functions associated with the event are called event handlers
EventArgs class - MSDN
- By convention an event uses a delegate that returns void and take 2 arguments:
- an object of type System.Object that contains a reference to the object that raised the event.
- an object from a class derived from EventArgs that contains the data passed from the object that raised the event to the event handlers.
- The EventArgs class does not contain any data by itself.
- So an event with no data will use a delegate of the form
public delegate void DelegateTypeName (object sender, EventArgs e)
- This simple event example is the same as the last one with the IntEventArgs class that serves for passing the int argument to the event handler and all the parameters changed to follow the calling convention for events.
- There are two special delegates defined in the System namespace to help you with the events declarations.
- The first is the EventHandler delegate. It passes no data. An event that passes no data can be declared as:
public event EventHandler EventName
- without having to declare a custom delegate.
- This is the usual way to declare an event that passes no data.
- The second is the EventHandler<T> generic delegate where T is a type derived from EventArgs
public event EventHandler<T> EventName
- again no need to declare a custom delegate type.
- This is the usual way to declare an event that passes data to the event handlers.
- Note that in this case you still have to declare the type T derived from EventArgs.
- This concludes the basic examples for events and delegates.