A Predicate delegate is an in-built generic type delegate. This delegate is defined under System namespace. It works with those methods which contain some set of criteria and determine whether the passed parameter fulfill the given criteria or not. This delegate takes only one input and returns the value in the form of true or false. Now, first of all, we see how custom delegates work in this situation. As shown in the below example.
public delegate bool Predicate <in P>(P obj);
Here, P is the type of the object and obj is the object which is going to compare against the criteria defined within the method represented by Predicate delegate.
Now, we use the same above program with Predicate delegate as shown below.
Example: In the below example, we use a predicate delegate instead of a custom delegate. It reduces the size of the code and makes the program more readable. Here, the Predicate delegate contains a single input parameter and return output in boolean type. And here, we directly assign a myfun method to the Predicate delegate.
- You can also use a Predicate delegate with an anonymous method as shown in the below example:
> val =
(mystring.Length < 7)
- You can also use a Predicate delegate with the lambda expressions as shown in the below example:
> val = str = > str.Equals(str.ToLower());
- C# | Action Delegate
- C# | Func delegate
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | Remove elements from a SortedSet that match the predicate
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Check if every List element matches the predicate conditions
- C# | Remove all elements of a List that match the conditions defined by the predicate
- Automation Using Selenium in C# With Example
- How to Execute C# Program on cmd (command-line)?
- How to Install and Setup Visual Studio for C#?
- What is .NET 3-Tier Architecture?
- Cross Join in LINQ
- Comparing two ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> in C#
- Compare two ValueTuple<T1, T2, T3, T4, T5, T6, T7> in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.