Delegates
A delegate is a type that references a method means it holds the address of method. It allows encapsulating a reference to a method inside a delegate object. A delegate can be assigned a method after which it can be used like any other method. You must define the delegate to match the signature of the method it will point to.
Delegates Overview
· Delegates are similar to C++ function pointers, but are type safe, object-oriented and secure.
· Delegates hold the address of one function or addresses of many functions.
· Delegates encapsulate some information like class names and methods names.
· If a Delegate is holding the address of only one function then it is called as single cast Delegate.
· If a Delegate is holding the addresses of many functions then it is called as Multicast Delegate.
· 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.
· Delegates can be used to invoke methods Asynchronously.
- Example 1 shows how to declare, instantiate, and call a single cast delegate.
- Example 2 shows working with multicast delegates.
Steps for declaring a delegate :-
- Write a class with methods:-
public class TestClass
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
}
- Create delegate [ delegate is a predefined keyword used to declare Delegate]:-
public delegate int MyDelegate(int x, int y);
- Create an object for the Delegate with the address of a function:-
MyDelegate del1 = new MyDelegate(TestClass.Add);
- Call the delegate:-
del1(5, 5);
Example 1
A Single Cast Delegate Usage Example
namespace SingleCastDelegate
{
//This delegate can point to any method,
//taking two integers and returning an
//integer.
public delegate int MyDelegate(int x, int y);
//This class contains methods that MyDelegate will point to.
public class TestClass
{
public static int Add(int x, int y)
{
return x + y;
}
public static int Multiply(int x, int y)
{
return x * y;
}
}
class Program
{
static void Main (string[] args)
{
//Create an Instance of MyDelegate
//that points to TestClass.Add().
MyDelegate del1 = new MyDelegate(TestClass.Add);
//Invoke Add() method using the delegate.
int addResult = del1(5, 5);
Console.WriteLine("5 + 5 = "+ addResult);
//Create an Instance of MyDelegate
//that points to TestClass.Multiply().
MyDelegate del2 = new MyDelegate(TestClass.Multiply);
//Invoke Multiply() method using the delegate.
int multiplyResult = del2(5, 5);
Console.WriteLine("5 X 5 = "+ multiplyResult);
Console.ReadLine();
}
}
}
Example 2
Delegate ability to Multicast
· Multicast Delegates holds the addresses of many functions.
· Multicast Delegates holds the sequence of functions.
· Multicast Delegates supports Arithmetic + and – operations.
· + operator adds a function into the sequence.
· - operator removes a function from the sequence.
A Multicast Delegate Example
namespace MulticastDelegate
{
//this delegate will be used to call more than one
//method at once
public delegate void MulticastDelegate(int x, int y);
//This class contains methods that MyDelegate will point to.
public class TestClass
{
//return type should be void in case of multicast delegates.
public static void Add(int x, int y)
{
Console.WriteLine("You are in Add() Method");
Console.WriteLine("{0} + {1} = {2}\n", x, y, x + y);
}
//return type should be void in case of multicast delegates.
public static void Multiply(int x, int y)
{
Console.WriteLine("You are in Multiply() Method");
Console.WriteLine("{0} X {1} = {2}", x, y, x * y);
}
}
class Program
{
static void Main (string[] args)
{
//Create an Instance of MulticastDelegate
//that points to TestClass.Add().
MulticastDelegate del = new MulticastDelegate(TestClass.Add);
//using the same instance of MulticastDelegate
//to call TestClass.Multiply() by adding it to it's
//invocation list.
//Invoke Add() and Multiply() methods using the delegate.
Console.WriteLine("****calling Add() and Multibly() Methods.****\n\n");
//removing the Add() method from the invocation list
Console.WriteLine("\n\n****Add() Method removed.****\n\n");
//this will invoke the Multiply() method only.
Console.ReadLine();
//Another way also you can use arithmetic operations with delegates.
MulticastDelegate d1, d2, d3, d4, d5, d6;
d1 = new MulticastDelegate(TestClass.Add);
d2 = new MulticastDelegate(TestClass.Multiply);
d3 = d1 + d2;
Console.WriteLine("\nFrom D3: ");
d3(5,5);
d4 = d3 + d3;
Console.WriteLine("\nFrom D4: ");
d4(5,5);
d5 = d4 - d1;
Console.WriteLine("\nFrom D5: ");
d5(5, 5);
d6 = d5 - d3 - d1;
Console.WriteLine("\nFrom D6: ");
d6(5, 5);
Console.ReadLine();
}
}
}
Note: Methods used with Multicast delegates should have return type as void. As if methods are returning something, then it would be big confusion that which method is returning which values, as there will be no means to find out, hence multicast delegates should only be used to with methods having return type void or which should not return anything.