How do you use delegates and / or events other than the user interface?

I am wondering how other developers use delegates and / or events other than responding to user interface events? I personally do not use them for anything other than responding to user interface events, but I have a strong feeling that I am losing sight of the delegate and event credentials. So I ask this question to the SO community so that I can get some ideas on how I could add these tools to my arsenal.

+4
source share
4 answers

The events are quite mundane. Most often, you will use them in response to a user interface or when developing components that should notify users of government changes. Yon.

The delegates, however, are full of fear and victory. Probably the most common use is in Linq. Linq uses lambdas all over the place that are abbreviated delegates.

var numbers = new int[]{1,2,3,4,5}; var evenStevens = numbers.Where(x => x % 2 == 0); 

Another common use is multithreading:

 ThreadPool.QueueUserWorkItem(o => DoWork(o)); 

Where I used them, which I like most, in the HtmlHelper extension methods that mix html rendering with codebehind:

  /// <summary> /// Helps render a simple list of items with alternating row styles /// </summary> /// <typeparam name="T">The type of each data item</typeparam> /// <param name="html">The HtmlHelper.</param> /// <param name="rows">The list of items</param> /// <param name="rowTemplate">The row template.</param> /// <param name="evenCssClass">The even row CSS class.</param> /// <param name="oddCssClass">The odd row CSS class.</param> public static void SimpleList<T>( this HtmlHelper html, IEnumerable<T> rows, Action<T, string> rowTemplate, string evenCssClass, string oddCssClass) { var even = false; foreach (var row in rows) { rowTemplate(row, even ? evenCssClass : oddCssClass); even = !even; } } 

An example of its use in an aspx file:

 <div id="nodes" class="scrollingBlock">> <% Html.SimpleList( Model.Nodes, (d, css) => {%> <div class='<%=css%>'><%=d.Name %></div> <%}, "evenRow", "oddRow");%> </div> 
+1
source

In any case, when you listen to events that may occur at any time; for example, while asynchronously listening on a socket, reading an XML file, etc.

+2
source

Events are perfect for any type of state machine you can configure. You fire another event for each state change, allowing your program to respond to these changes.

Another use for an event may mean that you will find out when a new record has been parsed or loaded from some long-term task. For example, you might have a controller object that loads a large XML file and parses records from it. Perhaps this controller fires an event every time it successfully parses a new record from a file.

0
source

I used events to inform some control class about the current state in a long running application.

In my case, I updated the Windows CE device with the new firmware, and this can take up to 15 minutes, so I would say that the controller knows when the line was updated, and it will update the progress bar, causing another event,

As already mentioned, if you need to know what is going on in another thread, events can be useful, especially if the operation can take a considerable amount of time.

0
source

All Articles