What are the advantages of delegates? [duplicate]

They’re a great way of encapsulating a piece of code. For instance, when you attach an event handler to the button, that handler is a delegate. The button doesn’t need to know what it does, just how to call it at the right time.

Another example is LINQ – filtering, projecting etc all require the same kind of template code; all that changes is the logic to represent the filter, the projection etc. With lambda expressions in C# 3 (which are converted into delegates or expression trees) this makes it really simple:

var namesOfAdults = people.Where(person => person.Age >= 18)
                          .Select(person => person.Name);

(That can also be represented as a query expression, but let’s not stray too far from delegates.)

Another way of thinking of a delegate is as a single-method interface type. For example, the EventHandler delegate type is a bit like:

public interface IEventHandler
{
    void Invoke(object sender, EventArgs e)
}

But the delegate support in the framework allows delegates to be chained together, invoked asynchronously, used as event handlers etc.

For more on delegates and events, see my article on the topic. Its focus is events, but it covers delegates too.

Leave a Comment