Delegates Basic
Delegates are references to methods. So far we have used references to objects , e.g . Stack st = new Stack ( );
Here st is a reference to an object of the Stack class type. Hence , each reference has two properties :
- The type of object ( class) the reference can point to .
- The actual object referenced ( or pointed to ) by the reference
Delegates are similar to object references, but are used to reference methods instead of objects. The type of a delegate is the type or signature of the method rather than the class. Hence a delegate has three properties.
- The type or signature of the method that the delegate can point to
- The delegate reference which can be used to reference a method
- The actual method referenced by the delegate
An object Reference ————————- An object of particular type
A Delegate ———————————— A method of particular type
The type or signature of the method the delegate can point to
Before using a delegate , we need to specify the type or signature of the method the delegate can reference. The signature of a method includes its return type and the type of parameters which it requires to be passed.
For example
int someMethod ( string [ ] args)
Is the common Signature of the Main ( ) method of a C# programs defined as:
int Main ( string [ ] args)
{
…
}
And for the following Add ( ) method :
int Add ( int a , int b)
{
return a+b;
}
The signature will be :
int aMethod ( int p, int q)
Which is also the signature of following Subtract ( ) method :
int Subtract ( int c , int d)
{
return c-d;
}
It should be noticed from the above examples that the name of a method is not the part of its signature ; the signature only involves its return type and parameters.
In case of delegates , we define the type of a delegate using the delegate keyword , e.g.
delegate int MyDelegate ( int p , int q );
Here we have defined a delegate type with the name ‘ MyDelegate’. The reference of this delegate type can be used to point to any method which takes two integers as parameters and returns an integer value.
The delegate reference, that can be used to reference a method
Once we have defined a delegate type, we can set it to reference actual methods with matching signatures . A delegate reference can be declared just like an object reference. For example, a reference of type MyDelegate ( defined above ) can be declared as :
MyDelegate arithMethod;
The delegate reference arithMethod can now reference any method whose signature is identical to the signature of MyDelegate.
3. The actual method referenced by the delegate
The delegate reference can be made to reference any method with a matching signature by passing its name as the parameter of delegate:
arithMethod = new MyDelegate ( Add);
Here, the arithMethod delegate reference is made to point to the Add ( ) method by passing its name as a parameter to the delegate type ( My Delegate ). The last two steps can be merged together in a single statement , like this :-
MyDelegate arithMethod = new MyDelegate ( Add);
Calling the actual method through its delegate
Once the delegate reference ‘ arithMethod’ has been made to point to the Add ( ) method, it can be used to call the actual method like this :-
int r = arithMethod ( 3 , 4);
The complete source code of the program is presented below.
using system;
namespace CSharpSchool
{
class Test
{
delegate int MyDelegate ( int p, int q);
static void Main( )
{
MyDelegate arithMethod = new MyDelegate ( Add );
int r = arithMethod (3 , 4);
console.WriteLine (” The result of arithmetic operation ‘ + ‘ on 3 and 4 is : {0}”,r);
}
static int Add ( int a, int b)
{
return a+b;
}
}
The result of arithmetic operation ‘+’ on 3 and 4 is : 7
Delegates in the .Net Framework
Although C# presents delegates as a keyword and as a first class language construct, in .Net delegates are present as a reference type, and all delegates inherit from the System. Delegate type. Hence, technically, our prior definition that said ‘ a delegate is a reference method ‘ is not quite appropriate . A delegate is a reference type derived from System.
class Test
{
delegate int MyDelegate (int p, int q);
static Void main ( )
{
MyDelegate arithMethod = null ;
….
}
}
Passing delegates to methods
Just like a reference to an object can be passed to other objects , the delegate reference of one method can be passed to another method. For example , Let make a method called ‘ PerformArithOperation ( )’, which takes two integers and a delegate reference of type MyDelegate, and calls the encapsulated method using the two integers.
static Void PerformArithOperation ( int a, int b, MyDelegate arithOperation)
{
int r = arithOperation (a , b);
Console.WriteLine (” \nThe result of arithmetic operation on 3 and 4 is : {0} “, r);
Now in the main ( ) method , we will call this method as
PerformArithOperation ( 3, 4, arithMethod );
Multicast Delegates
A special feature of delegates is that a single delegate can encapsulate more than one method of a matching signature. these kind of delegates are called ‘ Multicast Delegates’. Internally, multicast delegates are sub – types of system. Multicast Delegate, which itself is a subclass of system . Delegate. The most important point to remember about multicast delegates , which itself is a subclass of system.Delegate.