Professional Documents
Culture Documents
Definition:
The idea of a delegate in a nutshell is as follows:
Explanation:
Delegates allow us to work with variables and parameters that contain methods
Thus, a delegate in C# defines a type, in the same way as a class defines a type. A delegate
reflects the signature of a set of methods, not including the method names, however. A
delegate is a reference type in C#. It means that values of a delegate type are accessed via
references, in the same way as an object of a class always is accessed via a reference. In
particular, null is a possible delegate value.
A delegate is a class that encapsulates a method signature. Although it can be used in any
context, it often serves as the basis for the event-handling model in C# but can be used in a
context removed from event handling (e.g. passing a method to a method through a delegate
parameter).
Since delegates are classes, they have instances (delegate objects). Such delegate instances
contain references to one or more methods. A delegate instance is connected to one or more
methods using a delegate constructor and later using simple operators. The methods that a
delegate instance is associated with (invocation list) are invoked through the delegate instance.
A delegate defines the signature (return type and sequence of parameter types) of the methods that
can be added to the invocation list of a delegate instance. If an attempt is made to add a method to
a delegate object’s invocation list that does not confirm precisely to the signature in the delegate
class, a compilation error is emitted.
Syntax for delegate declaration is:
An example of invoking the method(s) on the invocation list of a delegate would be the
following:
The return value is always associated with the last method in the invocation list, Method2 in this
case. Often delegates are associated with methods that return void (commands) so there is no
return value to be concerned about.
Like all classes, delegates can be top-level classes within a namespace or be nested within an
existing class. The same access modifiers that can be applied to classes apply for delegates.
Delegates can contain generic parameters or return type.
Instantiating Delegates:
Once a delegate type has been declared, a delegate object must be created with the new
keyword and be associated with a particular method. When creating a delegate, the argument
passed to the new expression is written like a method call, but without the arguments to the
method. For example:
public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
Following example demonstrates declaration, instantiation and use of a delegate that can be used
to reference methods that take an integer parameter and returns an integer value.
using System;
When the above code is compiled and executed, it produces the following result:
Value of Num: 35
Value of Num: 175
Multicasting of a Delegate
Delegate objects can be composed using the "+" operator. A composed delegate calls the two
delegates it was composed from. Only delegates of the same type can be composed. The "-"
operator can be used to remove a component delegate from a composed delegate.
Using this useful property of delegates you can create an invocation list of methods that will
be called when a delegate is invoked. This is called multicasting of a delegate.
The following program demonstrates multicasting of a delegate:
using System;
When the above code is compiled and executed, it produces the following result:
Value of Num: 75
Use of delegates:
There are 6 important uses of delegates:-
2. Callback mechanism: Many times we would like to provide a call back mechanism. Delegates can be
passed to the destination and destination can use the same delegate pointer to make callbacks.
4. Multicasting - Sequential processing: Sometime we would like to call some methods in a sequential
manner which can be done by using multicast delegate. This is already explained in the multicast
example shown above.
5. Events - Publisher subscriber model: we can use events to create a pure publisher / subscriber
model.