What are delegates?
Hi everyone, from my what I understand, delegates are something similar to methods. So... what's the difference between a delegate and a method?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Feb 11, 2015, 11:43 PM
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type.
So it is function pointer not the function...
Please refer
https://msdn.microsoft.com/en-IN/library/ms173171.aspx
hope this will help you.
Pranay RanaPosted Feb 11, 2015, 9:30 AM
MahaPosted Feb 10, 2015, 3:39 PM
https://www.youtube.com/watch?v=9K9Aq7die7Q
Pranay RanaPosted Feb 10, 2015, 2:27 PM
There are two kind of delegate
1. Single cast delegate - this kind of delegate points only to one method
delegate void Print(string str);
static void Main(string[] args)
{
Print printstring = Printstring;
printstring.Invoke("Hello World!!");
Console.Read();
}
static void Printstring(string str) { Console.WriteLine(str); }
2. multi cast delegate - this kind of delegate points to multiple methods using + sign.
delegate void Print(string str);
static void Main(string[] args)
{
Print printstring += Printstring;
printstring += PrintWithName;
//below statment invoke both method assign to delegate
printstring.Invoke("Hello World!!");
Console.Read();
}
static void Printstring(string str) { Console.WriteLine(str); }
static void PrintWithName(string str) { Console.WriteLine("Pranay" + str); }
So main difference between method and delegate is
1. delegate can point to method or set of methods
2. With the help of delegate you can pass method as agrument to another function
3. With the delegate you can hold reference to method or methods , that you can by calling invoke method . if delegate point to mutiple method it runs all methods in one go.
you can read about it more here : http://www.codeproject.com/Articles/624575/Delegate-Tutorial-for-Beginners
Advance type of delegate : http://pranayamr.blogspot.in/2012/08/delegate-and-action-and-func-and-lamda.html