With real examples and their use, can someone please help me understand:
- When do we need a
Func<T, ..>delegate? - When do we need an
Action<T>delegate? - When do we need a
Predicate<T>delegate?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference between
FuncandActionis simply whether you want the delegate to return a value (useFunc) or not (useAction).Funcis probably most commonly used in LINQ – for example in projections:or filtering:
or key selection:
Actionis more commonly used for things likeList<T>.ForEach: execute the given action for each item in the list. I use this less often thanFunc, although I do sometimes use the parameterless version for things likeControl.BeginInvokeandDispatcher.BeginInvoke.Predicateis just a special casedFunc<T, bool>really, introduced before all of theFuncand most of theActiondelegates came along. I suspect that if we’d already hadFuncandActionin their various guises,Predicatewouldn’t have been introduced… although it does impart a certain meaning to the use of the delegate, whereasFuncandActionare used for widely disparate purposes.Predicateis mostly used inList<T>for methods likeFindAllandRemoveAll.