Delegates: Predicate vs. Action vs. Func

Sasha picture Sasha · Feb 19, 2009 · Viewed 31.7k times · Source

Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates:

  • Predicate
  • Action
  • Func

Answer

Jon Skeet picture Jon Skeet · Feb 19, 2009
  • Predicate: essentially Func<T, bool>; asks the question "does the specified argument satisfy the condition represented by the delegate?" Used in things like List.FindAll.

  • Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.

  • Func: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.

Other important delegates:

  • EventHandler/EventHandler<T>: Used all over WinForms

  • Comparison<T>: Like IComparer<T> but in delegate form.