Declaring a Delegate


A delegate is a special type that can store a function. When you declare a delegate you tell the compiler what types of functions your delegate is meant to store. The delegate declaration itself looks just like a function declaration but with the delegate keyword in front.

To declare a delegate:

  1. Either outside of a class or inside of a class type delegate .

  2. Write a function prototype; that is, type a return type, followed by a function name , followed by input parameters.

  3. Type a semicolon ; ( Figure 10.5 ).

    Figure 10.5 A delegate looks like a function declaration, except that the compiler turns the declaration into an entire class. The class name will be TaskDel, therefore TaskDel becomes a new data type.
     delegate bool TaskDel(string desc); 

graphics/tick.gif Tips

  • Just like other function declarations, delegates can either return void, or any other type.

  • At first glance it may look strange to throw a declaration of a delegate outside of a class, but the compiler takes that one line and creates a whole class out of it, so in essence what you are doing is declaring a new class. The class that it creates is derived from System.MulticastDelegate.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net