Delegates
hi
what is delegate in C# and how it is used and why should we use delegates
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
Lalit MRank #1,186 · 1.5K reputationPosted Dec 2, 2009, 7:21 PM EST
When use of Delegates?
This is a valid question. Before C# 3.0, you could use delegates or declare full methods to bind to events. Now we can declare event directly through lambda. (See this post on many different examples on how to bind event handlers).
Jon Skeet answered me the following:
Delegates is a keyword that can be used to declare inline methods. This inline code can be stored inside variables and then executed when necessary. This is exactly what happens when you are binding methods to events. You are storing the signature of the method inside a variable that will store multiple methods signature and call them when an event is happening.
Of course, it's limiting to think about delegates only as events. If we check the standard definition for the word delegation:
As I already demonstrated inside the StreamProxy class, we can easily give another software the tools to answer it's solution. But sometimes, the call might not be necessary. Just like when you are sending a data repository to a service class to save a model, delegate is basically just sending any method that match the accepted signature instead of complete class.
One of the most recent use of Lambda's inside C# is inside Mocking tools. Moq use those to easily describe expectations, returned values, and so on. This allow Moq to be type safe instead of relying on Reflection and string comparison. This has brought us compile time check rather than runtime check.
There is a lot of use for delegates and they are being used more and more. Lots of languages support some form of delegation (.NET, C++, JAVA, and many more)
Why Use of Delegates? Click HereShow this articles understanding delegates
Amit ChoudharyRank #145 · 15.8K reputationPosted Dec 2, 2009, 7:18 PM EST
You should go to this article http://www.exforsys.com/tutorials/csharp/delegates-in-csharp.html I'm sure all questions regarding Delegates will be answered
paul daniel189 reputationPosted Dec 23, 2009, 4:25 AM EST
This link will helpful to u
http://www.codersource.net/csharp_delegates_events.html
Rekha SinghRank #1,571 · 1K reputationPosted Dec 28, 2009, 2:42 AM EST
in delegates we have to pass the method name as parameter to our delegate
naveen33 reputationPosted Sep 2, 2010, 12:35 AM EDT
i just wanted bring your notice that in your post ,
Why Use of Delegates? Click Here
Click here ,link did not work.Page cannot found ERROR occurs
check with that upload right link
thanks
naveen33 reputationPosted Sep 2, 2010, 12:40 AM EDT
the link u provided also didn't work
http://www.codersource.net/csharp_delegates_events.html
Plz check out and upload proper one
thanks