When would you use delegates in C#?

Maxime Rouiller picture Maxime Rouiller · Oct 10, 2008 · Viewed 40.3k times · Source

What are your usage of delegates in C#?

Answer

Jon Skeet picture Jon Skeet · Oct 10, 2008

Now that we have lambda expressions and anonymous methods in C#, I use delegates much more. In C# 1, where you always had to have a separate method to implement the logic, using a delegate often didn't make sense. These days I use delegates for:

  • Event handlers (for GUI and more)
  • Starting threads
  • Callbacks (e.g. for async APIs)
  • LINQ and similar (List.Find etc)
  • Anywhere else where I want to effectively apply "template" code with some specialized logic inside (where the delegate provides the specialization)