views:

89

answers:

4

I am curious as to how other developers use delegates and/or events other than for responding to UI events? I, personally, don't use them for anything other than responding to UI events but I have a strong feeling that I am missing out on the power of delegates and events. So I pose this question to the SO community so that I may get some ideas on how I could possibly add these tools to my arsenal.

Thanks in advance!

+2  A: 

In any event where you are listening for events that may appear at any time; such as when asynchronously listening to a socket, reading an XML file, etc.

Williham Totland
A: 

Events are perfect for any sort of state machine you might set up. You fire a different event for each state change, allowing your program to react to those changes.

Another use for an event might be to let you know when a new record has been parsed or downloaded from some long-running task. For example, you might have a controller object that downloads a large XML file and parses records from it. You might have that controller fire an event every time it successfully parses a new record from the file.

Randolpho
A: 

I have used events to inform some controlling class as to the current state in a long running application.

In my case I was updating a windows CE device with new firmware, and it could take up to 15 minutes, so I would let the controller know when a line was updated, and it would update the progress bar by calling another event.

As was mentioned, if you need to know what is going on in a different thread then events can be helpful, especially if the operation can take substantial time.

James Black
+1  A: 

Events are pretty mundane. Most often you'll be using them in response to UI or when developing components that have to inform users about state changes. Yawn.

Delegates, however, are full of awesome and win. Probably the most common use is in Linq. Linq uses lambdas all over the place, which are shorthand delegates.

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

Another common use is in multithreading:

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

Where I've used them that I like the most is in HtmlHelper extension methods that mix rendering html 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>
Will