Home | » | C# | » | Delegates and Events |
---|
Delegates are reference types, which allow indirect calls to methods. A delegate instance holds references to a number of methods. By invoking the delegate one caused all of these methods to be called. The usefulness of delegates lies in the face that the functions, which invoke them are blind to the underlying methods they thereby cause to run.
It can be seen that delegates are functionally rather similar to C++'s function pointers. However, it is important to bear in mind two main differences. Firstly delegates are reference types rather than values types. Secondly, some single of delegates can reference multiple methods.
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces, typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).
Contents
Using Delegates
Delegates can be specified on their own in a namespace, or else can be specified within another class. In each case, the declaration specifies a new class, which inherits from System.MulticastDelegate.Multicast Delegate
Multicasting is a technique that allows an object call any number of functions. It is derived from MulticastDelegate class. A new function can be added to the delegate list, the - MulticastDelegate.Combine() - method is used. The general syntax of the same is:
MulticastDelegate.Combine(delegate1, delegate2);
Note that since + operator is overloaded, the above can also be written as :
MulticastDelegate mdel = delegate1 + delegate2;
Creating a multicast delegate variable - mdel.
Similarly, a delegate can be removed from the delegate list using - MulticastDelegate.Remove() - method whose general syntax is:
MulticastDelegate.Remove(delegate1);
The following program show MulticastDelegate.
Events
An event is C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).
Events, however, need not be used only for graphical interfaces. Events provide a generally useful way for objects to signal state changes that may be useful to clients of that objects. Events are an important building block for creating classes that can be reused in a large number of different programs.
Events are declared using delegates. An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. When the event occurs, the delegate(s) given to it by its clients are invoked. To declare an event inside a class, first a delegate type for event must be declared. The following program show event in C#.