Delegates: Predicate vs. Action vs. Func
Solution 1:
Predicate
: essentiallyFunc<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 WinFormsComparison<T>
: LikeIComparer<T>
but in delegate form.
Solution 2:
Action
, Func
and Predicate
all belong to the delegate family.
Action
: Action can take n input parameters but it returns void.
Func
: Func can take n input parameters but it will always return the result of the provided type. Func<T1,T2,T3,TResult>
, here T1,T2,T3 are input parameters and TResult is the output of it.
Predicate
: Predicate is also a form of Func but it will always return bool. In simple words it is wrapper of Func<T,bool>
.