Thursday, August 12, 2010

What is a Delegate?

What is a Delegate?
delegates
are function pointers that point to function of matching signatures.

On the other hand .NET framework has introduced a type-safe mechanism called delegates, with automatic verification of the signature by the compiler.

While using delegates it is very much necessary to make sure that the functions which the delegates points has the same number of argument type and same return type. For example if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.

Types of Delegate

Delegates are of two types:

Single cast delegate

A delegate is called single cast delegate if it invokes a single method. In other words we can say that SingleCast Delegates refer to a single method with matching signature.

Multicast Delegates

MultiCast Delegates are nothing but a single delegate that can invoke multiple methods of matching signature. MultiCast Delegate derives from System.MulticastDelegate class which is a subclass of System.Delegate.


For example if we are required to call two methods on a single button click event or mouse over event then using MultiCast Delegates we can easily call the methods.

No comments:

Post a Comment