What is the use of delegate Predicate<T> ,Shall i handle it as nested style like
Func<Predicate<T>>?. Simple example please.
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.
There are several use cases where its useful to allow the caller to provide a function that returns
trueorfalse. ThePredicate<T>type is a way to do this. This is no different thanFunc<T, bool>(exceptPredicate<T>was available in earlier versions of C#).A simple (and contrived example):
or, lambda style:
The
FindAllmethod returns an array of all items that match the condition. In this case, all the strings that start with “h”. ThePredicateiss.StartsWith("h").The useful thing here, is that the
Arrayclass knows how to search through all its elements, and create a new array containing only the elements that match. This part of the algorithm is common to many pieces of code. The part that is not common, is the matching criteria, which will vary based on the requirements of the specific piece of code. Thus the caller specifies that part of the logic, and passes it in as aPredicate.You can handle it in a “nested” style.
Func<Predicate<T>>means you are declaring a function that returns aPredicate<T>. Are you sure this is what you want? What are you trying to achieve?