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

CHAPTER 15 DELEGATES

Creating the Delegate Object

A delegate is a reference type and therefore has both a reference and an object. After a delegate type is declared, you can declare variables and create objects of the type. The following code shows the declaration of a variable of a delegate type:

Delegate type

Variable

MyDel

delVar;

There are two ways you can create a delegate object. The first is to use an object-creation expression with the new operator, as shown in the following code. The operand of the new operator consists of the following:

The delegate type name.

A set of parentheses containing the name of a method to use as the first member in the invocation list. The method can be either an instance method or a static method.

Instance method

 

 

 

 

 

 

 

 

 

 

 

delVar

=

new

MyDel(

myInstObj.MyM1 );

//

Create

delegate

and

save

ref.

dVar

=

new

MyDel(

SClass.OtherM2 );

//

Create

delegate

and

save

ref.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Static method

 

 

 

 

 

 

You can also use the shortcut syntax, which consists of just the method specifier, as shown in the following code. This code and the preceding code are equivalent. Using the shortcut syntax works because there is an implicit conversion between a method name and a compatible delegate type.

delVar

=

myInstObj.MyM1;

//

Create

delegate

and

save

reference.

dVar

=

SClass.OtherM2;

//

Create

delegate

and

save

reference.

373

CHAPTER 15 DELEGATES

For example, the following code creates two delegate objects—one with an instance method and the other with a static method. Figure 15-4 shows the instantiations of the delegates. This code assumes that there is an object called myInstObj, which is an instance of a class that has defined a method called MyM1 returning no value and taking an int as a parameter. It also assumes that there is a class called SClass, which has a static method OtherM2 with a return type and signature matching those of delegate MyDel.

delegate

void MyDel(int x);

// Declare delegate type.

MyDel delVar, dVar;

// Create two delegate

variables.

 

 

 

Instance method

 

 

 

 

 

 

 

 

delVar =

new MyDel( myInstObj.MyM1 );

// Create delegate and

save ref.

dVar

=

new MyDel( SClass.OtherM2 );

// Create delegate and

save ref.

 

 

 

 

 

 

 

Static method

Figure 15-4. Instantiating the delegates

Besides allocating the memory for the delegate, creating a delegate object also places the first method in the delegate’s invocation list.

You can also create the variable and instantiate the object in the same statement, using the initializer syntax. For example, the following statements also produce the same configuration shown in Figure 15-4:

MyDel

delVar =

new

MyDel(

myInstObj.MyM1

);

MyDel

dVar

=

new

MyDel(

SClass.OtherM2

);

The following statements use the shortcut syntax but again produce the results shown in Figure 15-4:

MyDel

delVar

=

myInstObj.MyM1;

MyDel

dVar

=

SClass.OtherM2;

374

CHAPTER 15 DELEGATES

Assigning Delegates

Because delegates are reference types, you can change the reference contained in a delegate variable by assigning to it. The old delegate object will be disposed of by the garbage collector (GC) when it gets around to it.

For example, the following code sets and then changes the value of delVar. Figure 15-5 illustrates the code.

MyDel delVar;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

delVar = myInstObj.MyM1;

//

Create

and

assign

the

delegate object.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

delVar = SClass.OtherM2;

//

Create

and

assign

the

new delegate object.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 15-5. Assigning to a delegate variable

375

CHAPTER 15 DELEGATES

Combining Delegates

All the delegates you’ve seen so far have had only a single method in their invocation lists. Delegates can be “combined” by using the addition operator. The result of the operation is the creation of a new delegate, with an invocation list that is the concatenation of copies of the invocation lists of the two operand delegates.

For example, the following code creates three delegates. The third delegate is created from the combination of the first two.

MyDel delA = myInstObj.MyM1;

MyDel delB = SClass.OtherM2;

MyDel delC = delA + delB;

// Has combined invocation list

Although the term combining delegates might give the impression that the operand delegates are modified, they are not changed at all. In fact, delegates are immutable. After a delegate object is created, it cannot be changed.

Figure 15-6 illustrates the results of the preceding code. Notice that the operand delegates remain unchanged.

Figure 15-6. Combining delegates

376

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