先看一个简单的delegate的例子:
1 public static bool IsOdd(int i)
2 {
3 return (i & 1) == 1;
4 }
5 public delegate bool NumberTester(int i);
6 public static void PrintMatchingNumbers(int from, int to, NumberTester filter)
7 {
8 for (int i = from; i <= to; ++i)
9 {
10 if (filter(i))
11 {
12 Console.WriteLine(i);
13 }
14 }
15 }
2 {
3 return (i & 1) == 1;
4 }
5 public delegate bool NumberTester(int i);
6 public static void PrintMatchingNumbers(int from, int to, NumberTester filter)
7 {
8 for (int i = from; i <= to; ++i)
9 {
10 if (filter(i))
11 {
12 Console.WriteLine(i);
13 }
14 }
15 }