tags:

views:

1843

answers:

3

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

What other delegates a csharp developer should be aware of?

How often do you use them in production code?

thanks

+2  A: 

Actually, I recently ran across this Blog entry which does a pretty good job.

Mystere Man
Looks good, I will print it out...
+17  A: 
  • 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.

Jon Skeet
just like I wanted +1
Perpetualcoder
There's also `System.Converter<TInput, TOutput>`, though it's rarely used.
gWiz
The Converter is a nice delegate when a lot of Converting of Model into Business classes is needed, i.e. http://www.stum.de/2009/12/23/using-a-converter-to-convert-from-a-model-to-a-business-class/
Michael Stum
A: 

In addition to Jon's answer, there is also

  • Converter<TInput, TOutput>: It's essentially Func<TInput, TOutput>, but with semantics. Used by List.ConvertAll and Array.ConvertAll, but personally haven't seen it anywhere else.
gWiz