Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer.
Advantages
- Encapsulating the method's call from caller
- Effective use of delegate improves the performance of application
- Used to call a method asynchronously
Declaration:
public delegate type_of_delegate delegate_name()
Example:
public delegate int mydelegate(int delvar1,int delvar2)
Note
- You can use delegates without parameters or with parameter list
- You should follow the same syntax as in the method
(If you are referring to the method with twoint
parameters andint
return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)
Sample Program using Delegate
public delegate double Delegate_Prod(int a,int b);
class Class1
{
static double fn_Prodvalues(int val1,int val2)
{
return val1*val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
Console.Write("Please Enter Values");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());
//use a delegate for processing
double res = delObj(v1,v2);
Console.WriteLine ("Result :"+res);
Console.ReadLine();
}
}
Explanation
Here I have used a small program which demonstrates the use of delegate.
The delegate "
Delegate_Prod
" is declared with double return type and accepts only two integer parameters.
Inside the class, the method named
fn_Prodvalues
is defined with double return type and two integer parameters. (The delegate and method have the same signature and parameter type.)
Inside the
Main
method, the delegate instance is created and the function name is passed to the delegate instance as follows:Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
After this, we are accepting the two values from the user and passing those values to the delegate as we do using method:
delObj(v1,v2);
Here delegate object encapsulates the method functionalities and returns the result as we specified in the method.
Example 2:
1. Defining the delegate
public delegate int Calculate (int value1, int value2);
2. Creating methods which will be assigned to delegate object
//a method, that will be assigned to delegate objects //having the EXACT signature of the delegatepublic int add(int value1, int value2) { return value1 + value2; } //a method, that will be assigned to delegate objects //having the EXACT signature of the delegatepublic int sub( int value1, int value2) { return value1 - value2; }
3. Creating the delegate object and assigning methods to those delegate objects
//creating the class which contains the methods //that will be assigned to delegate objectsMyClass mc = new MyClass(); //creating delegate objects and assigning appropriate methods //having the EXACT signature of the delegateCalculate add = new Calculate(mc.add); Calculate sub = new Calculate(mc.sub);
4. Calling the methods via delegate objects
//using the delegate objects to call the assigned methods Console.WriteLine("Adding two values: " + add(10, 6)); Console.WriteLine("Subtracting two values: " + sub(10,4));
Thanks...