Delegate is a simple class that is used to point to methods with a specific signature, becoming essentially a type-safe function pointer. A delegate's purpose is to facilitate a call back to another method (or methods), after one has been completed, in a structured way
Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer.
Declaration
Creating a delegate is easy to do. Identify the class as a delegate with the "delegate" keyword. Then specify the signature of the type.
public delegate type_of_delegate delegate_name()
Example.
public delegate double Integrand(double x);
public delegate double Delegate_Prod(int a,int b);
class ClassDel
{
static double fn_Prodvalues(int val1,int val2)
{
return val1*val2;
} static void Main(string[] args) {
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();
}
}
.
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
Creating a delegate is easy to do. Identify the class as a delegate with the "delegate" keyword. Then specify the signature of the type.
public delegate type_of_delegate delegate_name()
Example.
public delegate double Integrand(double x);
public delegate double Delegate_Prod(int a,int b);
class ClassDel
{
static double fn_Prodvalues(int val1,int val2)
{
return val1*val2;
} static void Main(string[] args) {
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();
}
}
.
What is Multicast Delegate?
It is a delegate which holds the reference of more than one method.
Multicast delegates must contain only methods that return
void
, else there is a run-time exception.
delegate void Delegate_Multicast(int x, int y);
Class Class2
{
static void Method1(int x, int y)
{
Console.WriteLine("You r in Method 1");
}
static void Method2(int x, int y)
{
Console.WriteLine("You r in Method 2");
}
public static void <place w:st="on" />Main</place />()
{
Delegate_Multicast func = new Delegate_Multicast(Method1);
func += new Delegate_Multicast(Method2);
func(1,2); // Method1 and Method2 are called
func -= new Delegate_Multicast(Method1);
func(2,3); // Only Method2 is called
}
}
Where use delegate?
- Event handlers (for GUI and more like Menu)
- Starting threads(like progress bar)
- Callbacks (e.g. for async APIs)
- LINQ and similar (List.Find etc)
- Anywhere else where I want to effectively apply "template" code with some specialized logic inside (where the delegate provides the specialization)
- Event Handling
- Multi Casting
- Filtering
Delegate types are derived from the Delegate class in the .NET Framework. Delegate types are sealed—they cannot be derived from— and it is not possible to derive custom classes from Delegate. Because the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property.
ReplyDelete