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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
MahaRank #2,573 · 543 reputationPosted Feb 10, 2015, 10:39 AM EST
https://www.youtube.com/watch?v=9K9Aq7die7Q
Pranay RanaRank #735 · 3.2K reputationPosted Feb 10, 2015, 9:27 AM EST
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
Pranay RanaRank #735 · 3.2K reputationPosted Feb 11, 2015, 4:30 AM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 11, 2015, 6:43 PM EST
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.