Introduction In this article I am going to share my knowledge on Delegates in C#.This would explain the Delegate using simple examples so that the beginner can understand the same.
What is Delegate? Definition:
Delegate is type which holds the method(s) reference in an object. It is also reffered as a type safe function pointers.
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 delegeate without parameter or with parameter list .You should follow the same syntax as in the method (If you are refering the method with two int parameters and int return type the delegate which you are declaring should be the same format.This is how it is refered 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 which 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 is having the same signature and parameters type)
Inside the Main method the delegate instance is created and the function name is passed to the delegate instance as following.
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 return the result as we specified in the method.
Multicast Delegate What is Multicast Delegate? : It is a Delegate which holds the reference of more than one methods. Multicast delegates must contain only methods that return void, else there is a run-time exception.
Simple Program using Multicast Delegate ----------------------------------------
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 Main() { 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 }
}
Explanation:
In the above example you can see that two methods are defined named method1 and method2 which takes two integer parameters and return type as void.
In the main method the Delegate object is created using the following statements
Delegate_Multicast func = new Delegate_Multicast(Method1);
Then the Delegate is added using the += operator and removed using -= operator.
Summary
This article would help you understand delegates using simple example.If you have any query regarding this article please catch me at arul_tony@yahoo.com.
|
| Author: V.Venkatesa Prasath 13 Oct 2005 | Member Level: Bronze Points : 0 |
Very nice . It 's Very useful to me. thanks once again. From PrasathVV , Coimbatore.
|
| Author: V.Venkatesa Prasath 13 Oct 2005 | Member Level: Bronze Points : 0 |
Very nice . It 's Very useful to me. thanks once again. From PrasathVV , Coimbatore.
|
| Author: Irfan 15 Mar 2006 | Member Level: Bronze Points : 0 |
Thanx a lot.... Its really very useful. ( Can u give the example of the Events n Delegates in practical llife )
|
| Author: Nagarajan 18 Dec 2006 | Member Level: Bronze Points : 0 |
Thanks for explaining in easy way to understand about delegates
|
| Author: Rupesh 27 Aug 2008 | Member Level: Bronze Points : 1 |
I got the idea how is working..... But.. Plz. make me understand. Why should I do ..................... double res = delObj(v1, v2);
If I can Just ..................... double res = fn_Prodvalues(v1, v2);
|