Using Delegates and Events
If you are familiar with Windows programming, you’ve most likely dealt with
callbacks. Callbacks are method calls that are executed when some event happens
http://www.syngress.com.70 Chapter 2 • Introducing C# Programming
during processing. For instance, a callback can be established to handle the pro-cessing
of an incoming message on a communications port. Another part of the
communications program can wait for messages on a communications port and
invoke the callback whenever a new message arrives. Function pointers perform
the same sort of tasks in straight C/C++ programs.
Delegates in C# improve on method callbacks in two areas. Delegates are type
safe, unlike callbacks in Windows programming. In addition, delegates can call
more than one callback when an event occurs. This is termed multicasting.
Delegates
Let’s extend our employees sample to use delegates.This sample simulates a back-ground
process that receives messages to add new employees to the employee list.
Our queue will be a static array, but in the real world it could be a message
queue (Microsoft Message Queue [MSMQ]), a socket, or some other type of
queue. The source code in Figure 2.7 shows the relevant portions of the sample
pertaining to delegates.The full source code for this sample is on the CD in the
file Delegates.cs.
Figure 2.7 Relevant Portions of the Delegates.cs Program Listing
using System;
using System.Collections;
/// <summary>
/// Contains the program entry point for the Delegates Sample.
/// </summary>
class DelegatesSample
{
static void Main( string[] args )
{
try
{
// Create a container to hold employees
Employees employees = new Employees(4);
// Create and drain our simulated message queue
EmployeeQueueMonitor monitor =
http://www.syngress.com
Continued.Introducing C# Programming • Chapter 2 71
new EmployeeQueueMonitor( employees );
monitor.start();
monitor.stop();
// Display the employee list on screen
Console.WriteLine(
"List of employees added via delegate:" );
foreach ( Employee employee in employees )
{
string name = employee.FirstName + " " +
employee.MiddleName + " " + employee.LastName;
string ssn = employee.SSN;
Console.WriteLine( "Name: {0}, SSN: {1}", name, ssn );
}
}
catch ( Exception exception )
{
// Display any errors on screen
Console.WriteLine( exception.Message );
}
}
}
/// <summary>
/// Simulates our message queue.
/// </summary>
class EmployeeQueueMonitor
{
// Delegate signature
http://www.syngress.com
Figure 2.7 Continued
Continued.72 Chapter 2 • Introducing C# Programming
public delegate void AddEventCallback( string FirstName,
string LastName, string MiddleName, string SSN );
// Instance of the delegate
private AddEventCallback m_addEventCallback;
private Employees m_employees;
private int m_lengthQueue;
private string[, ] m_msgQueue =
{
{"Timothy", "Arthur", "Tucker", "555-55-5555"},
{"Sally", "Bess", "Jones", "666-66-6666" },
{"Jeff", "Michael", "Simms", "777-77-7777"},
{"Janice", "Anne", "Best", "888-88-8888" }
};
public EmployeeQueueMonitor( Employees employees )
{
m_employees = employees;
m_lengthQueue = 4;
// Create an instace of the delegate and register the
// addEmployee method of this class as a callback.
m_addEventCallback = new AddEventCallback(
this.addEmployee );
}
// Drain the queue.
public void start()
{
if ( m_employees == null )
return;
http://www.sy