FREE BOOK

Chapter 12 - Delegates and Lambda Expressions

Posted by Addison Wesley Free Book | LINQ October 13, 2009
C# achieves the same functionality using a delegate, which encapsulates methods as objects, enabling an indirect method call bound at runtime.

Delegate Instantiation in C# 1.0
 
Earlier versions of the compiler require instantiation of the delegate demonstrated in Listing 12.9
 
 Listing 12.9: Passing a Delegate Instance as a Parameter Prior to C# 2.0
 
 public delegate bool ComparisonHandler (int first, int second);
 
class DelegateSample
 
{
 
public static void BubbleSort(
 
int[] items, ComparisonHandler comparisonMethod)
 {
 
// ...
 
}
 BubbleSort(items, GreaterThan);
 
public static bool GreaterThan(int first, int second)
 {
 
return first > second;
 }
 
static void Main(string[] args)
 {
 
int i;
 
int[] items = new int[5];
 
for (i=0; i<items.Length; i++)
 {
 
Console.Write("Enter an integer:");
 items[i] = int.Parse(Console.ReadLine());
 }
 
for (i = 0; i < items.Length; i++)
 {
 
Console.WriteLine(items[i]);
 }
 }
 
// ...
 
}
 
Note that C# 2.0 and above support both syntaxes, but unless you are writing backward-compatible code, the 2.0 syntax is preferable. Therefore, throughout the remainder of the book, I will show only the C# 2.0 and above syntax. (This will cause some of the remaining code not to compile on version 1.0 compilers without modification to use explicit delegate instantiation.)
 
The approach of passing the delegate to specify the sort order is significantly more flexible than the approach listed at the beginning of this chapter. With the delegate approach, you can change the sort order to be alphabetical simply by adding an alternative delegate to convert integers to strings as part of the comparison. Listing 12.10 shows a full listing that demonstrates alphabetical sorting, and Output 12.1 shows the results.
 
 
BubbleSort(items,new ComparisonHandler(GreaterThan));
 

 Listing 12.10: Using a Different ComparisonHandler-Compatible Method
 

 
using System;
 
class DelegateSample
 
{
 
public delegate bool ComparisonHandler(int first, int second);
 
public static void BubbleSort(
 
int[] items, ComparisonHandler comparisonMethod)
 {
 
int i;
 
int j;
 
int temp;
 
for (i = items.Length - 1; i >= 0; i--)
 {
 
for (j = 1; j <= i; j++)
 {
 
if (comparisonMethod(items[j - 1], items[j]))
 {
 temp = items[j - 1];
 items[j - 1] = items[j];
 items[j] = temp;
 }
 }
 }
 }
 
public static bool GreaterThan(int first, int second)
 {
 
return first > second;
 }
 
static void Main(string[] args)
 {
 
int i;
 
int[] items = new int[5];
 
for (i=0; i<items.Length; i++)
 {
 
public static bool AlphabeticalGreaterThan(
 
int first, int second)
 {
 
int comparison;
 comparison = (first.ToString().CompareTo(
 second.ToString()));
 
return comparison > 0;
 }
 
Console.Write("Enter an integer: ");
 items[i] = int.Parse(Console.ReadLine());
 }
 
for (i = 0; i < items.Length; i++)
 {
 Console.WriteLine(items[i]);
 }
 }
 
}
 
The alphabetic order is different from the numeric order. Note how simple it was to add this additional sort mechanism, however, compared to the process used at the beginning of the chapter.
 
The only changes to create the alphabetical sort order were the addition of the AlphabeticalGreaterThan method and then passing that method into the call to BubbleSort().

Total Pages : 9 12345

comments