Delegates - passing a reference to one method as an argument to another method.
Delegates - allow you to capture a reference to a method and pass it around like any other object, and to call the captured method like any other method.
classDelegateSample { publicdelegateboolComparisonHandler(int first, int second);
publicstaticvoidBubbleSort( int[] items, ComparisonHandler comparisonMethod) { int i; int j; int temp;
if (comparisonMethod == null) { thrownew ArgumentNullException("comparisonMethod"); }
if (items == null) { return; }
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; } } } }
publicstaticboolGreaterThan(int first, int second) { return first > second; }
publicstaticboolAlphabeticalGreaterThan(int first, int second) { int comparison; comparison = (first.ToString().CompareTo(second.ToString()));
return comparison > 0; }
staticvoidMain() { int i; int[] items = newint[5];
for (i = 0; i < items.Length; i++) { Console.Write("Enter an integer: "); items[i] = int.Parse(Console.ReadLine()); }