|
Delegate is a type safe Functional Pointer. it hold method or methods reference in an object.effective use of delegate to improve performance of application.
Declaration
public delegate type_of_delegate delegate_name(var_type var1,var_type var2)
Note :-
- you can use delegate with parameters or without parameters
- you should follow the same syntax as in the method
Sample program using Delegate
//delegate Declaration
public delegate double delegate_prod(int a,int b)
Class Class1
{
//method with the same parameters and same signature
static double fnprodvalue(int val1,int val2)
{
return var1*var2;
}
static void main(string[] args)
{
//creating delegate instance
delegate_prod objDelegate =new delgate_prod(fnprodvalue);
double res = objDelegate(5,4);
console writeline("Result :", + res);
console.readline():
}
}
Delegates are two types
single caste -single caste delegate refers only one function address after creating the instance variable
multi-caste delegate- multi-caste refers to multi-single caste delegate features
|