Skip to content

Declaring the Delegate Type

As we stated in the previous section, 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:

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.

The preceding declaration specifies that delegate objects of type MyDel will only accept methods that have a single int parameter and that have no return value. Below figure shows a representation of the delegate type on the left and the delegate object on the right.

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

 

Note: Even though the delegate type declaration looks like a method declaration, it doesn’t need to be declared inside a class because it’s a type declaration.

Leave a Reply

Your email address will not be published.