Книга: C# 2008 Programmer

Creating a Delegate

Creating a Delegate

To understand the use of delegates, begin by looking at the conventional way of invoking a function. Consider the following program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Delegates {
 class Program {
  static void Main(string[] args) {
   int num1 = 5;
   int num2 = 3;
   Console.WriteLine(Add(num1, num2).ToString());
   Console.WriteLine(Subtract(num1, num2).ToString());
   Console.ReadLine();
  }
  static int Add(int num1, int num2) {
   return (num1 + num2);
  }
  static int Subtract(int num1, int num2) {
   return (num1 - num2);
  }
 }
}

The program contains three methods: Main(), Add(), and Subtract(). Notice that the Add() and Subtract() methods have the same signature. In the Main() method, you invoke the Add() and Subtract() methods by calling them directly, like this:

Console.WriteLine(Add(num1, num2).ToString());
Console.WriteLine(Subtract(num1, num2).ToString());

Now create a delegate type with the same signature as the Add() method:

namespace Delegates {
 class Program {
  delegate int MethodDelegate(int num1, int num2);
  static void Main(string[] args) {
   ...

You define a delegate type by using the delegate keyword, and its declaration is similar to that of a function, except that a delegate has no function body.

To make a delegate object point to a function, you create an object of that delegate type (MethodDelegate, in this case) and instantiate it with the method you want to point to, like this:

static void Main(string[] args) {
 int num1 = 5;
 int num2 = 3;
 MethodDelegate method = new MethodDelegate(Add);

Alternatively, you can also assign the function name to it directly, like this:

MethodDelegate method = Add;

This statement declares method to be a delegate that points to the Add() method. Hence instead of calling the Add() method directly, you can now call it using the method delegate:

//---Console.WriteLine(Add(num1, num2).ToString());---
Console.WriteLine(method(num1, num2).ToString());

The beauty of delegates is that you can make the delegate call whatever function it refers to, without knowing exactly which function it is calling until runtime. Any function can be pointed by the delegate, as long as the function's signature matches the delegate's.

For example, the following statements check the value of the Operation variable before deciding which method the method delegate to point to:

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)
 Console.WriteLine(method(num1, num2).ToString());

You can also pass a delegate to a method as a parameter, as the following example shows:

using System;
using System.Collections.Generic;
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.

Оглавление книги


Генерация: 1.712. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз