Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 15 DELEGATES

Adding Methods to Delegates

Although you saw in the previous section that delegates are, in reality, immutable, C# provides syntax for making it appear that you can add a method to a delegate, using the += operator.

For example, the following code “adds” two methods to the invocation list of the delegate. The methods are added to the bottom of the invocation list. Figure 15-7 shows the result.

MyDel delVar

=

inst.MyM1;

// Create and initialize.

delVar

+=

SCl.m3;

//

Add

a

method.

delVar

+=

X.Act;

//

Add

a

method.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 15-7. Result of “adding” methods to a delegate. In reality, because delegates are immutable, the

resulting delegate with three methods in its invocation list is an entirely new delegate pointed at by

the variable.

What is actually happening, of course, is that when the += operator is used, a new delegate is created, with an invocation list that is the combination of the delegate on the left plus the method listed on the right. This new delegate is then assigned to the delVar variable.

377

CHAPTER 15 DELEGATES

Removing Methods from a Delegate

You can also remove a method from a delegate, using the -= operator. The following code shows the use of the operator. Figure 15-8 shows the result of this code when applied to the delegate illustrated in Figure 15-7.

delVar -= SCl.m3;

// Remove the method from the delegate.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 15-8. Result of removing a method from a delegate

As with adding a method to a delegate, the resulting delegate is actually a new delegate. The new delegate is a copy of the old delegate—but its invocation list no longer contains the reference to the method that was removed.

The following are some things to remember when removing methods:

If there are multiple entries for a method in the invocation list, the -= operator starts searching at the bottom of the list and removes the first instance of the matching method it finds.

Attempting to delete a method that is not in the delegate has no effect.

Attempting to invoke an empty delegate throws an exception.

You can check whether a delegate’s invocation list is empty by comparing the delegate to null. If the invocation list is empty, the delegate is null.

378

CHAPTER 15 DELEGATES

Invoking a Delegate

You invoke a delegate by calling it, as if it were simply a method. The parameters used to invoke the delegate are used to invoke each of the methods on the invocation list (unless one of the parameters is an output parameter, which we’ll cover shortly).

For example, the delegate delVar, as shown in the following code, takes a single integer input value. Invoking the delegate with a parameter causes it to invoke each of the members in its invocation list with the same parameter value (55, in this case). Figure 15-9 illustrates the invocation.

MyDel delVar

=

inst.MyM1;

delVar

+=

SCl.m3;

delVar

+=

X.Act;

...

 

 

 

 

 

 

 

 

delVar( 55 );

 

// Invoke the delegate.

...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 15-9. When the delegate is invoked, it executes each of the methods in its invocation list, with the

same parameters with which it was called.

A method can be in the invocation list more than once. If it’s in the list more than once, then when the delegate is invoked, the method will be called each time it is encountered in the list.

379

CHAPTER 15 DELEGATES

Delegate Example

The following code defines and uses a delegate with no parameters and no return value. Note the following about the code:

Class Test defines two print functions.

Method Main creates an instance of the delegate and then adds three more methods.

The program then invokes the delegate, which calls its methods. Before invoking the delegate, however, it checks to make sure it’s not null.

//Define a delegate type with no return value and no parameters.

delegate void PrintFunction();

class

Test

 

 

{

 

 

 

public

void Print1()

 

{

Console.WriteLine("Print1 -- instance"); }

public

static void Print2()

 

{

Console.WriteLine("Print2 -- static"); }

}

 

 

 

class

Program

 

{

 

 

 

static

void Main()

 

{

Test t = new Test();

// Create a test class instance.

 

 

PrintFunction pf;

// Create a null delegate.

 

pf

= t.Print1;

// Instantiate and initialize the delegate.

//Add three more methods to the delegate. pf += Test.Print2;

pf += t.Print1;

pf += Test.Print2;

//The delegate now contains four methods.

if( null != pf )

//

Make sure the delegate isn't null.

pf();

//

Invoke the delegate.

else

 

 

Console.WriteLine("Delegate is empty");

}

}

This code produces the following output:

Print1 -- instance

Print2 -- static

Print1 -- instance

Print2 -- static

380

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]