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

C H A P T E R 15

■ ■ ■

Delegates

What Is a Delegate?

Declaring the Delegate Type

Creating the Delegate Object

Assigning Delegates

Combining Delegates

Adding Methods to Delegates

Removing Methods from a Delegate

Invoking a Delegate

Delegate Example

Invoking Delegates with Return Values

Invoking Delegates with Reference Parameters

Anonymous Methods

Lambda Expressions

369

CHAPTER 15 DELEGATES

What Is a Delegate?

A delegate is a user-defined type, like a class. But whereas a class represents a collection of data, a delegate keeps track of one or more methods. You use a delegate by doing the following. We’ll go through each of these steps in detail in the following sections.

1.Declare a new delegate type with a particular signature and return type. A delegate declaration looks like a method declaration, except that it doesn’t have an implementation block.

2.Declare a delegate variable of the new delegate type.

3.Create an object of the delegate type, and assign it to the delegate variable. The new delegate object includes a reference to a method with the same signature as defined in the first step.

4.Add additional methods into the delegate object. These methods must have the same signature and return type as the delegate type defined in the first step.

5.Throughout your code you can then invoke the delegate, just as it if it were a method. When you invoke the delegate, each of the methods it contains is executed.

In looking at the previous steps, you might have noticed that they’re similar to the steps in creating and using a class. Figure 15-1 compares the processes of creating and using classes and delegates.

Figure 15-1. A delegate is a user-defined reference type, like a class.

Note If you're coming from a C++ background, the fastest way for you to understand delegates is to think of them as type-safe, object-oriented C++ function pointers on steroids.

370

CHAPTER 15 DELEGATES

You can think of a delegate as an object that contains an ordered list of methods with the same signature and return type, as illustrated in Figure 15-2.

The list of methods is called the invocation list.

Methods held by a delegate can be from any class or struct, as long as they match both the delegate’s

Return type

Signature (including ref and out modifiers)

Methods in the invocation list can be either instance methods or static methods.

When a delegate is invoked, each method in its invocation list is executed.

Figure 15-2. A delegate as a list of methods

371

CHAPTER 15 DELEGATES

Declaring the Delegate Type

Delegates are types, just as classes are types. And as with classes, a delegate type must be declared before you can use it to create variables and objects of the type. The following example code declares a delegate type.

Even though the delegate type declaration looks like a method declaration, it does not need to be declared inside a class because it is a type declaration.

Keyword

Delegate type name

delegate void MyDel ( int x );

The declaration of a delegate type looks much like the declaration of a method, in that it has both a return type and a signature. The return type and signature specify the form of the methods that the delegate will accept.

For example, the following code declares delegate type MyDel. This declaration specifies that delegates of this type will only accept methods that have a single int parameter and that have no return value. Figure 15-3 shows a representation of the delegate type on the left and the delegate object on the right.

Delegate type name

delegate void MyDel( int x );

 

Return type

Signature

Figure 15-3. Delegate type and object

The delegate type declaration differs from a method declaration in two ways. The delegate type declaration

Is prefaced with the keyword delegate

Does not have a method body

372

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