Information Technology Reference
In-Depth Information
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 create variables and objects of the type. The following example
code declares a delegate type.
The delegate type declaration, as with all type declarations, does not need to be declared
inside a class.
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
. The declaration specifies
that objects of this type will accept only methods that return no value and have a single
int
parameter. Figure 15-2 shows a representation of the delegate type on the left, and the object
on the right.
Delegate type name
↓
delegate void
MyDel( int x )
;
↑
↑
Return type
Signature
Figure 15-2.
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