+ 1

Delegates.

Can someone please explain to, in a dummy way, delegates and why do we need them? I've looked at it on Google, but it's still not yhat clear. Thanks in advance.

6th Sep 2017, 9:59 AM
Shahar Levy
Shahar Levy - avatar
7 Antworten
+ 6
I don't know C# much, but as I see it, it is used for asynchronous operation, say, for example, your code's primary task is to calculate employees salary, but while doing that, it needs to read records from the database, by using delegates your code can continue its work, there's no need to wait for the delegate, the delegate will notify your code when it has done its job (reading records from database). Without delegates your code will have to switch task to reading records from its primary task. And because the primary task is abandoned, your code becomes ineffective in work, and inefficient in time. Even more simple example, just imagine you are a manager, director or the like, at work, delegates are your subordinates, you can tell them to do tasks, so that your primary task won't be interrupted, and you tell them they must report to you upon completion. Everybody's happy, job finished on schedule, nobody gets fired :) Hth, cmiiw
6th Sep 2017, 11:08 AM
Ipang
+ 4
Part 2 of 2: Here is a basic scenario to help further explain delegates: ** Imagine the following scenario: ** - When a new answer is posted in SoloLearn, the member activity feed and the main home activity feed are both updated. Without events and callbacks via delegates, the Questions class would need to make explicit method calls such as: class SoloLearnQnA { public void AnswerPosted(string message){ var memberFeed = new SoloLearnMemberFeed(); memberFeed.UpdateActivityFeed(message); var homeFeed = new SoloLearnHomeFeed(); homeFeed.UpdateActivityFeed(message); } } Delegates, on the other hand, would allow for the following alternative: //Define delegate reference type. public delegate void ActivityFeedHandler(string message); class SoloLearnQnA { //Declare an event based on the delegate type. public event ActivityFeedHandler OnActivityUpdated; public void AnswerPosted(string message){ //Raise the event OnActivityUpdated(message); } } //Implements handler for the member activity feed. class SoloLearnMemberFeed { private SoloLearnQnA questions = new SoloLearnQnA(); public SoloLearnMemberFeed() { //Register the callback method matching the delegate signature questions.OnActivityUpdated += this.UpdateActivityFeed; } public void UpdateActivityFeed(string message){ //Update the member feed. } } //Implements handler for the home activity feed. class SoloLearnHomeFeed { private SoloLearnQnA questions = new SoloLearnQnA(); public SoloLearnHomeFeed() { //Register the callback method matching the delegate signature questions.OnActivityUpdated += this.UpdateActivityFeed; } public void UpdateActivityFeed(string message){ //Update the member feed. } } I could further enhance this code by creating a base class or interface for the UpdateActivityFeed() method. Hopefully, this pseudo code and simple example helps provide some context around how delegates work.
8th Sep 2017, 6:39 AM
David Carroll
David Carroll - avatar
+ 3
You're welcome, glad to help
6th Sep 2017, 11:21 AM
Ipang
+ 3
Part 1 of 2: It might help to understand that .NET supports 3 kinds of reference types: - class, interface, and delegate. - Classes define all properties and method signatures, as well as, the implementation logic of a reference type. - Interfaces are simpler in that only public method signatures and public properties are defined. - Delegates are the simplest in that only a stand-alone method signature is defined. Example delegate: public delegate void SomeCallbackMethod(object sender, EventArgs args); This very basic reference type is what allows methods to be registered with event handlers or passed as parameters to other functions and be used as callbacks in .NET. I recommend the following links for some really good examples: - http://csharp-station.com/Tutorial/CSharp/Lesson14 - http://sellsbrothers.com/public/writing/delegates.htm (Explained as a story.) - https://www.codeproject.com/Articles/38716/Delegates-Explained-in-Plain-English - https://www.tutorialspoint.com/csharp/csharp_delegates.htm - https://www.akadia.com/services/dotnet_delegates_and_events.html - https://www.intertech.com/Blog/c-sharp-tutorial-understanding-c-events/
8th Sep 2017, 6:40 AM
David Carroll
David Carroll - avatar
+ 2
Thank you very much.
6th Sep 2017, 11:18 AM
Shahar Levy
Shahar Levy - avatar
+ 1
Thank you very much DavidC
8th Sep 2017, 6:33 AM
Shahar Levy
Shahar Levy - avatar