Tuesday, 11 December 2012

Delegates

Delegates are the ultimate function pointer. Developers familiar with C and C++ are very familiar with function pointers and their lack of instance-based knowledge. A delegate can be thought of as a call back mechanism, essentially saying, "Please invoke this method for me when the time is right."
Consider the following scenario: Your department has just hired a new employee. Human Resources needs to be notified when a new employee is hired so they can put them to sleep with endless paper work and boring drivel about company policies and practices. This type of interaction is a perfect example of a delegate. Basically, Human Resources is requesting a notification when a new employee is hired and provides a method to be invoked.
Listing 2.1.32 Using Delegates shows the basic use of delegates.
Listing 2.1.32 Using Delegates
 1: //File        :part02_31.cs
 2: //Author    :Richard L. Weeks
 3: //Purpose    :Demonstrate the use of delegates
 4:
 5: using System;
 6:
 7: //Define a person struct
 8: public struct Person {
 9:     public string FName;
10:     public string LName;
11: }
12:
13: //Define a delegate
14: public delegate void OnNewHire( Person person );
15:
16: //The HR Class
17: public class HR {
18:
19:     //Implement the delegate to be called when a new person is hired
20:     public void OnNewHire( Person person ) {
21:         Console.WriteLine( "HR is in the process of putting {0}  to sleep", 
graphics/ccc.gifperson.FName );
22:     }
23: }
24:
25: //Create a department
26: public class Department {
27:
28:     //Who to notify
29:     private OnNewHire    m_OnNewHireDelegate = null;
30:
31:     //set the OnNewHire delegate
32:     public void AddOnNewHireDelegate( OnNewHire onh ) {
33:         m_OnNewHireDelegate = onh;
34:     }
35:
36:
37:     public void HirePerson( Person p ) {
38:         //do we need to notify someone?
39:         if( m_OnNewHireDelegate != null )
40:             m_OnNewHireDelegate( p );
41:     }
42: }
43:
44:
45: public class DelegateTest {
46:
47:     public static void Main( ) {
48:
49:         HR hr = new HR( );
50:         Department dept = new Department( );
51:
52:         //Register the OnNewHire Delegate
53:         dept.AddOnNewHireDelegate( new OnNewHire( hr.OnNewHire ) );
54:
55:         //Create a person
56:         Person me; me.FName = "Richard"; me.LName = "Weeks";
57:
58:         //Hire ME!!!
59:         dept.HirePerson( me );
60:     }
61: }
Listing 2.1.32 implements the HR scenario and makes use of a delegate to notify HR when a new person has been hired. The delegate OnNewHire is defined on line 14. Notice the use of the delegate keyword to denote what is being declared. Remember that C# does not allow for global methods, so C# would issue an error without the delegate keyword.
The HR class provides a handler for the delegate. The method name does not have to be the same as the name of the delegate; this was done to make it easier to follow. The Department class provides a method AddOnNewHireDelegate to handle the "hooking-up" of the delegate with the intended handler. Notice the call on line 53 that actually adds the HR handler to the Department. A delegate is a type in C# and requires an instance, hence the use of the new keyword to create a new delegate.
I would encourage any developer to explore delegates in detail because their use in .NET is prolific, especially in Windows Forms development.

No comments:

Post a Comment

Comment Here