Delegates and Events

Together, delegates and events help connect code with user interface actions. Think of a button-click in a Windows Forms application. The button has a Click event, a delegate references a method, and the delegate is attached to that event, telling that button to run a specific method when it's clicked. There are other uses of delegates and events also, but capturing user interface events is the most prevalent, which is why you should understand how they work.

Delegates

A delegate is a reference type that enables a reference to a method to be passed around and used as needed. The code that receives the delegate reference doesn't know or care what the underlying method is or what it does. The code just calls the method because whoever gave the reference wants the method to be called when an event occurs. It's like the Click event of a button. The button doesn't care what code is executed when someone clicks it. All delegates attached to the button at the time the click occurred are invoked. Listing 4.3 demonstrates how to define and use delegates.

Listing 4.3 Definition and Use of a Delegate (UsingDelegates.cs)
 using System; public delegate void MyDelegate(); class UsingDelegates {    static void Main()    {       MyDelegate del = new MyDelegate(CallbackMethod);       // invoke delegate       del();       Console.ReadLine();    }    public static void CallbackMethod()    {       Console.WriteLine("CallbackMethod.");    } } 

Listing 4.3 declares a new delegate type, MyDelegate. It then instantiates the delegate, del, in the Main method, specifying CallbackMethod as the method to run when the delegate is invoked. By using del just like a method call, CallbackMethod is actually invoked. The delegate instance, del, could have been passed to a method, put in an array, or reassigned just like any other type.

The example used the assignment operator to assign a new delegate to del. If another delegate was assigned to del, the old value of del would be released and del would hold the callback method of the new delegate.

It is also possible to add multiple delegates to a single delegate, using the += syntax. This would mean that when del is invoked, the callback method for each of the delegates would be invoked. Because of the possibility of accidentally assigning, instead of adding, delegates are not the safest way to perform notifications to multiple callbacks. This would be a better job for events.

Events

An event is a type member that is invoked when something related to that event happens. This is much like the Click event that fires when someone clicks a button with the mouse.

Events are essentially delegates with restrictions. Delegates can only be added to or removed from an event, using the += and -= operators, respectively. Also, events can only be invoked from within the type where they are defined. On the other hand, a delegate can be reassigned and invoked from any code that has access to it. In a positive light, an event is less of a restriction and more of a protection for a type. Listing 4.4 shows the code from Listing 4.3 after it has been rewritten to use events.

Listing 4.4 Definition and Use of an Event (UsingEvents.cs)
 using System; public delegate void MyDelegate(); class Listing0404 {    public static event MyDelegate MyEvent;    static void Main()    {       MyEvent += new MyDelegate(CallbackMethod);       // invoke event       MyEvent();       Console.ReadLine();    }    public static void CallbackMethod()    {       Console.WriteLine("CallbackMethod.");    } } 

Listing 4.4 is functionally equivalent to Listing 4.3, except that it uses an event. It declares MyEvent, which is of type MyDelegate. In Main, a new delegate referencing CallbackMethod is added to MyEvent. Delegates can only be added to an event using the += syntax, making events safer to work with.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net