Wednesday, September 8, 2010

DELEGATES AND EVENTS

Delegates
Delegates are a construct for abstracting and creating objects that reference methods and can be used to call those methods. Delegates form the basis of event handling in C#. A delegate declaration specifies a particular method signature. References to one or more methods can be added to a delegate instance. The delegate instance can then be "called" which effectively calls all the methods that have been added to the delegate instance. A simple example:


delegate void Procedure();

class DelegateDemo
{
static void Method1()
{
Console.WriteLine("Method 1");
}
static void Method2()
{
Console.WriteLine("Method 2");
}
void Method3()
{
Console.WriteLine("Method 3");
}

static void Main()
{
Procedure someProcs = null;
someProcs += new Procedure(DelegateDemo.Method1);
someProcs += new Procedure(DelegateDemo.Method2);
DelegateDemo demo = new DelegateDemo();
someProcs += new Procedure(demo.Method3);
someProcs();
}
}

In this example, the delegate is declared by the line delegate void Procedure();
This statement is a complete abstraction. It does not result in executable code that does any work. It merely declares a delegate type called Procedure which takes no arguments and returns nothing. Next, in the Main() method, the statement Procedure someProcs = null; instantiates a delegate. Something concrete has now been created.
The assignment of someProcs to null means that it is not initially referencing any methods. The statements someProcs += new Procedure(DelegateDemo.Method1); and someProcs += new Procedure(DelegateDemo.Method2);
add two static methods to the delegate instance. (Note: the class name could have been left off of DelegateDemo.Method1 and DelegateDemo.Method2 because the statement is occurring in the DelegateDemo class.)
The statement someProcs += new Procedure(demo.Method3); adds a non-static method to the delegate instance.

For a non-static method, the method name is preceded by an object reference. When the delegate instance is called, Method3() is called on the object that was supplied when the method was added to the delegate instance. Finally, the statement someProcs(); calls the delegate instance. All the methods that were added to the delegate instance are now called in the order that they were added.

Methods that have been added to a delegate instance can be removed with the -= operator:
someProcess -= new Procedure(DelegateDemo.Method1);
In C# 2.0, adding or removing a method reference to a delegate instance can be shortened as follows:
someProcess += DelegateDemo.Method1;
someProcess -= DelegateDemo.Method1;
Invoking a delegate instance that presently contains no method references results in a NullReferenceException.
Note that if a delegate declaration specifies a return type and multiple methods are added to a delegate instance, then an invocation of the delegate instance returns the return value of the last method referenced. The return values of the other methods cannot be retrieved (unless explicitly stored somewhere in addition to being returned).

Events
An event is a special kind of delegate that facilitates event-driven programming. Events are class members which cannot be called outside of the class regardless of its access specifier. So, for example, an event declared to be public would allow other classes the use of += and -= on the event, but firing the event (i.e. invoking the delegate) is only allowed in the class containing the event. A simple example:


delegate void ButtonClickedHandler();
class Button
{
public event ButtonClickedHandler ButtonClicked;
public void SimulateClick()
{
if (ButtonClicked != null)
{
ButtonClicked();
}
}
...
}


A method in another class can then subscribe to the event by adding one of its methods to the event delegate:
Button b = new Button();
b.ButtonClicked += MyHandler;
Even though the event is declared public, it cannot be directly fired anywhere except in the class containing the event.
Events are used extensively in GUI programming and in the System.Windows.Forms namespace.

No comments:

Post a Comment

Followers