using System.Linq;

using System.Text;

namespace Delegates {

 class Program {

  delegate int MethodDelegate(int num1, int num2);

  static void PerformMathOps(MethodDelegate method, int num1, int num2) {

   Console.WriteLine(method(num1, num2).ToString());

  }

  static void Main(string[] args) {

   int num1 = 5;

   int num2 = 3;

   char Operation = 'A';

   MethodDelegate method = null;

   switch (Operation) {

   case 'A':

    method = new MethodDelegate(Add);

    break;

   case 'S':

    method = new MethodDelegate(Subtract);

    break;

   }

   if (method != null)

    PerformMathOps(method, num1, num2);

   Console.ReadLine();

  }

  static int Add(int num1, int num2) {

   return (num1 + num2);

  }

  static int Subtract(int num1, int num2) {

   return (num1 - num2);

  }

 }

}

In this example, the PerformMathOps() function takes in three arguments — a delegate of type MethodDelegate and two integer values. Which method to invoke is determined by the Operation variable. Once the delegate is assigned to point to a method (Add() or Subtract()), it is passed to the PerformMathOps() method.

Delegates Chaining (Multicast Delegates)

In the previous section, a delegate pointed to a single function. In fact, you can make a delegate point to multiple functions. This is known as delegates chaining. Delegates that point to multiple functions are known as multicast delegates.

Consider the following example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Delegates {

 class Program {

  delegate void MethodsDelegate();

  static void Main(string[] args) {

   MethodsDelegate methods = Method1;

   methods += Method2;

   methods += Method3;

   //---call the delegated method(s)---

   methods();

   Console.ReadLine();

  }

  static private void Method1() {

   Console.WriteLine('Method 1');

  }

  static private void Method2() {

   Console.WriteLine('Method 2');

  }

  static private void Method3() {

   Console.WriteLine('Method 3');

  }

 }

}

This program three methods: Method1(), Method2(), and Method3(). The methods delegate is first assigned to point to Method1(). The next two statements add Method2() and Method3() to the delegate by using the += operator:

MethodsDelegate methods = Method1;

methods += Method2;

methods += Method3;

When the methods delegate variable is called, the following output results:

Method 1

Method 2

Method 3

The output shows that the three methods are called in succession, in the order they were added.

What happens when your methods each return a value and you call them using a multicast delegate? Here's an example in which the three methods each return an integer value:

class Program {

 delegate int MethodsDelegate(ref int num1, ref int num2);

 static void Main(string[] args) {

  int num1 = 0, num2 = 0;

  MethodsDelegate methods = Method1;

  methods += Method2;

  methods += Method3;

  //---call the delegated method(s)--- 

  Console.WriteLine(methods(ref num1, ref num2));

  Console.WriteLine('num1: {0} num2: {1}', num1, num2);

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату