Wednesday, June 10, 2009

Delegate

Delegate
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked
The signature of a single cast delegate is shown below:
delegate result-type identifier ([parameters]);
where:
result-type: The result type, which matches the return type of the function.
identifier: The delegate name.
parameters: The Parameters, that the function takes.
Delegates have the following properties:
• Delegates are similar to C++ function pointers, but are type safe.
• Delegates allow methods to be passed as parameters.
• Delegates can be used to define callback methods.
• Delegates can be chained together; for example, multiple methods can be called on a single event.
• Methods don't need to match the delegate signature exactly. For more information, see Covariance and Contravariance
• C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method.

No comments:

Post a Comment