![]() |
VOOZH | about |
In C#, a Predicate is a built-in delegate type used to represent a method that takes one input parameter and returns a boolean value (true or false). It is widely used for testing conditions, especially in filtering data with collections.
Output:
True
False
The Predicate<int> delegate takes an integer and checks if it is even. It returns true for 10 and false for 7.
public delegate bool Predicate<in T>(T obj);
Output:
True
False
The method CheckPositive matches the Predicate signature (takes one parameter, returns bool). So it can be directly assigned to a Predicate delegate.
Predicates are very useful in collection methods like List<T>.Find, FindAll, Exists and RemoveAll.
Output:
First Even: 2
All Even Numbers:
2
4
6
Output:
Passed Students:
Alice - 85
Charlie - 60
Here the Predicate checks whether a student has marks greater than or equal to 50. The FindAll method uses this Predicate to return only passing students.