Console.ReadLine();

 }

 static private int Method1(ref int num1, ref int num2) {

  Console.WriteLine('Method 1');

  num1 = 1;

  num2 = 1;

  return 1;

 }

 static private int Method2(ref int num1, ref int num2) {

  Console.WriteLine('Method 2');

  num1 = 2;

  num2 = 2;

  return 2;

 }

 static private int Method3(ref int num1, ref int num2) {

  Console.WriteLine('Method 3');

  num1 = 3;

  num2 = 3;

  return 3;

 }

}

When the methods delegate is called, Method1(), Method2(), and Method3() are called in succession. However, only the last method (Method3()) returns a value back to the Main() function, as the output shows:

Method 1

Method 2

Method 3

3

num1: 3 num2: 3

If one of the methods pointed to by a delegate causes an exception, no results are returned.

The following modifications to the preceding program shows that Method2() throws an exception and is caught by the try-catch block:

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;

  try {

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

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

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

  } catch (Exception ex) {

   Console.WriteLine(ex.Message);

  }

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

  Console.ReadLine();

 }

 static private int Method1(ref int num1, ref int num2) {

  Console.WriteLine('Method 1');

  num1 = 1;

  num2 = 1;

  return l;

 }

 static private int Method2(ref int num1, ref int num2) {

  throw new Exception();

  Console.WriteLine('Method 2');

  num1 = 2;

  num2 = 2;

  return 2;

 }

 static private int Method3(ref int num1, ref int num2) {

  Console.WriteLine('Method 3');

  num1 = 3;

  num2 = 3;

  return 3;

 }

}

The following output shows that num1 and num2 retain the values set by the last method that was successfully invoked by the delegate:

Method 1

Exception of type 'System.Exception' was thrown.

num1: 1 num2: 1

Just as you use the += operator to add a method to a delegate, you use the - = operator to remove a method from a delegate:

static void Main(string[] args) {

 int num1 = 0, num2 = 0;

 MethodsDelegate methods = Method1;

 methods += Method2;

 methods += Method3;

 //...

 //...

 //---removes Method3---

 methods -= Method3;

Implementing Callbacks Using Delegates

One of the useful things you can do with delegates is to implement callbacks. Callbacks are methods that you pass into a function that will be called when the function finishes execution. For example, you have a function that performs a series of mathematical operations. When you call the function, you also pass it a callback method so that when the function is done with its calculation, the callback method is called to notify you of the calculation result.

Following is an example of how to implement callbacks using delegates:

class Program {

 delegate void callbackDelegate(string Message);

 static void Main(string[] args) {

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

0

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

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