Delegate Goal

Duplicate:

The difference between events and delegates and related applications

What are the benefits of delegates?

Where can I use delegates?

I wonder what the purpose of the delegates is. I have not used them so much and can’t think of anything.

It is written in my courses that the delegate is a blue font for all methods corresponding to its signature.

In addition, you can add several methods to one delegate, and then they will be executed after each of them in the order in which they were added. This is probably only useful for methods that affect local variables or methods that do not return any values.

I read that C # implements events as delegates, which are documented as:

//Summary: Represents the method that will handle an event that has no event data. //Parameters: //sender: The source of the event. //e: An System.EventArgs that contains no event data. [Serializable] [ComVisible(true)] public delegate void EventHandler(object sender, EventArgs e); 

However, this is a bit confusing. Can someone give a good, useful example of this concept?

+20
c # delegates
Mar 26 '09 at 21:32
source share
7 answers

Yes,

You are almost there. The delegate refers to a method or function that needs to be called..NET uses events to say .. when someone clicks this button, I want you to execute this part of the code.

For example, when using the GPS application:

 public delegate void PositionReceivedEventHandler(double latitude, double longitude); 

This suggests that the method should accept two doubles as inputs and return a void. When we come to the definition of an event:

 public event PositionReceivedEventHandler PositionReceived; 

This means that the PositionRecieved event fires a method with the same definition as the PositionReceivedEventHandler delegate defined. Therefore when you do

 PositionRecieved += new PositionReceivedEventHandler(method_Name); 

The name of the method must match the delegate, so that we know how to execute the method, what parameters it expects. If you use the Visual Studio constructor to add some events to the button, for example, all this will work on a delegate that expects an object and an EventArgs parameter.

Hope this helps ...

+25
Mar 26 '09 at 21:38
source share

As you noted, a delegate is a way to create a signature for calling a method. There are many great examples of using delegates, but the one that really opened my mind is an example.

 public delegate Duck GetDuckDelegate(); public GetDuckDelegate GiveMeTheDuckFactoryMethod(string type) { switch(type) { case "Rubber": return new GetDuckDelegate(CreateRubberDuck); case "Mallard": return new GetDuckDelegate(CreateMallardDuck); default: return new GetDuckDelegate(CreateDefaultDuck); } } public Duck CreateRubberDuck() { return new RubberDuck(); } public Duck CreateMallardDuck() { return new MallardDuck(); } public Duck CreateDefaultDuck() { return new Duck(); } 

Then to use it

 public static void Main() { var getDuck = GiveMeTheDuckFactoryMethod("Rubber"); var duck = getDuck(); } 

A Factory template might be the best way to do this, but I just came up with this example on the fly and thought it proved that delegates can be thought of as objects

+13
Mar 26 '09 at 21:42
source share

Delegates let you pass methods around such values.

For example, .Net has a method called Array.ForEach that takes a delegate and an array and calls a delegate for each element of the array.

Therefore you can write

 int[] arr = new int[] { 1, 2, 4, 8, 16, 32, 64 }; Array.ForEach(arr, new Action<int>(Console.WriteLine)); 

This code will call Console.WriteLine for each number in the array.

There are many things you can do by creating functions that delegates accept, especially when combined with anonymous methods. For example, see LINQ .

+3
Mar 26 '09 at 21:48
source share

I can provide you an example using web application architecture:

Typically, using a web application, you can provide a front controller that receives requests from many clients. We could put all our methods in the front controller to work with various types of requests from clients. However, it gets a little cumbersome. Instead, we can use delegates to encapsulate functions for different requests. We could:

  • Authentication delegate
  • User Management Delegation

etc. Thus, this is an easy way to split functionality into logical chunks - delegates. The Struts structure is based on this way of working (ActionServlet and Action classes).

+2
Mar 26 '09 at 21:38
source share

There are many great articles explaining delegates - here are some good ones:

Delegates and Events
C # delegate explanation
C # delegates

+2
Mar 26 '09 at 21:39
source share

Many people are initially confused with the real need for delegates and events. I was one of them, and it took me a while to figure this out :-). I recently answered a similar request in ASP.NET forums and thought it would be nice if I created a blog post on this topic! Here is the request:

“I read an example somewhere in the bank class that if the minimum balance is reached, you need to tell the rest of the application that the mines have been reached, but we can’t do this simply by calling the usual method. For example: say, when we subtract some amount out of balance, and if the minimum is reached, then call some method to take some action, I absolutely don’t understand why we need delegates and user events here? "

The thing in the Bank case, you can definitely call a method, but then it would be just procedural programming, we need event-based programming when we want our code to respond to some events generated by the system.

For example: think that Windows is a system, and we write code (in any language) where we want to capture an event, such as mouse_click (). Now how does our program know that a mouse click has occurred? We can use low-level code for it, but since the OS is already processing low-level code, it is best to capture the event raised by the OS.

In other words, the moment mouse_click () occurs, the OS fires an event. The OS does not care about who captures this event and uses it, it just sends a notification. Then any code (for example, ours) can capture this event and use it accordingly. This saves us a lot of time to write code for the same. And other programs can also use the same event and process it accordingly.

Similarly, the banking system can be huge and many other external applications can access it. The banking system does not know how many such applications are in it, or depends on it, and how they will handle certain situations, for example, when the balance is low, so it just fires an event when a low balance occurs, and this event can be used by any other code, in addition to the bank code itself.

Note that each susbcriber for this event can handle this event independently, for example. the bank code may stop something from being executed if the balance is low, some other reporting application may send an email in this case, or some ATM code may stop the transaction of a particular participant and inform the user that the balance is low.

Hope this clears a bit!

+2
Oct 20 '09 at 7:54
source share

Delegates, as far as I know, provide a way to specialize the behavior of a class without subclassing it.

Some classes have complex general behavior, but are still intended for specialized ones. Think of the Window class in a graphical interface: a window can use a lot, but you most likely still want to specialize it somehow. In some frameworks, this is done through inheritance. Another way to do this is with delegates. Suppose you want something to happen when the window changes: your delegate class can then implement a method called onWindowResize (assuming, of course, that the Window class supports this), which is called whenever the window changes and is responsible for any specialized behavior when the window is resized.

I will not argue about the advantages of delegation over inheritance, but suffice it to say that many people believe that delegation is “cleaner” than inheritance.

+1
Mar 26 '09 at 21:53
source share



All Articles