Delegate in C#
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 Delegat 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 reffering 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 reffered 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 interger 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 delegae instace 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
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 whchi takes two integer parameters and return type as void.
In the main method the Delegate object is created using the following statement
Delegate_Multicast func = new Delegate_Multicast(Method1);
Then the Delegate is added using the += operator and removed using -= operator.
Conclusion.
This article would help you understand the Delegates in C# .
suresh kumarPosted Feb 27, 2013, 1:32 AM
Really it's very nice and Easy to understand thanq..........
venkatesuluPosted Jun 24, 2011, 3:00 PM
Author simplified the concepts and is successful in explaining them. Keep good work man, I am difinite that many people will benefit from this, including me.
Naren ChejaraPosted Mar 22, 2011, 5:02 AM
Thank you. I am completely understand delegate to read this article. keep writing such kind of Article so every on can read it Once again thanks
Bharadwa HirenPosted Jan 28, 2011, 1:10 AM
Really Nice one and Simple Helped me
Chetan RaoPosted May 26, 2010, 6:35 AM
Very nice article.I learnt what i wanted in delegate.First time i understood what is delegate and multicast delgate.
April Pierson DrakePosted Sep 9, 2009, 5:46 PM
I'm appalled by the rude comments you've gotten...I think your article was very nice and YES we're here to learn C# not spelling :) thanks for the clear simple description that finally made this concept clear to me.
vibs gisPosted Oct 16, 2007, 3:43 AM
HZi Arul, really a gud attempt to explain delegates. its really gud , no other explanation could as simple as this one. thnks, vibs
OanaPosted Mar 26, 2007, 8:22 AM
Really now, this is one of the best articles i have read in ages. It's clearly meant for beginners and it's exactly what a beginner wants: a short, simple and neat explanation of what something is/does. Good enough to give one end of the rope. Thumbs up.
Ted BissellPosted Feb 25, 2006, 8:31 PM
While the actual code written in this article has some value, the mis-spellings and grammatical errors in the English of the rest of the article make it most annoying to read and more difficult to understand the definition of what a delegate actually is. A far better description can be found here http://www.softsteel.co.uk/tutorials/cSharp/lesson16.html; at least for those of us that speak, read and write English not as a second language.