What is a predicate in c?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, a predicate is simply a function that answers a yes-or-no question about a value. The built-in Predicate<T> delegate represents a method that takes one value of type T and returns bool. Predicates are used everywhere filtering or matching happens, especially in collection APIs such as Find, FindAll, and RemoveAll.
The Basic Definition
Predicate<T> has this shape:
That means the method receives one value and returns either true or false.
Here is a simple example:
The predicate does not change the input. It only evaluates it.
Where Predicates Are Used
Predicates show up in many .NET collection methods.
These APIs are readable because the predicate expresses the rule directly.
Predicate<T> Versus Func<T, bool>
Many developers ask whether Predicate<T> is different from Func<T, bool>. In practice, they represent the same logical shape: one input, one boolean result.
Example with Func<T, bool>:
So why does Predicate<T> exist? Mostly because older .NET APIs were designed with that delegate type specifically. Newer code often uses Func<T, bool> more often, especially with LINQ.
Predicate Usage with LINQ
LINQ commonly uses Func<T, bool> rather than Predicate<T>, but conceptually it is still the same yes-or-no idea.
That lambda is acting as a predicate even though the parameter type is inferred as Func<string, bool>.
Write Named Predicates for Reuse
If the condition is used more than once, giving it a name often improves clarity.
Named predicates are especially useful when the rule is meaningful to the domain and deserves explanation through naming.
Use Predicates for Business Rules Carefully
Predicates are great for simple checks, but they should remain readable. If the rule becomes large or depends on many services, do not hide all that complexity inside an anonymous lambda.
Readable:
Harder to maintain:
Once the condition grows, a named method or specification object is usually better.
Predicates and Side Effects
A predicate should ideally be pure: same input, same boolean answer, and no side effects. Collection APIs assume the condition is only checking, not mutating.
Bad example:
This may still run, but now the predicate has extra behavior that makes reasoning harder.
Common Pitfalls
The most common confusion is thinking Predicate<T> is a special language keyword. It is just a delegate type defined by .NET.
Another issue is overusing inline predicates until the calling code becomes unreadable. Long lambdas hide business rules instead of clarifying them.
Developers also sometimes mutate state inside predicates. That is legal, but it makes filtering code harder to test and reason about.
Summary
- A predicate in C# is a function that takes one value and returns
bool. - '
Predicate<T>is the built-in delegate type for that yes-or-no check.' - Collection APIs such as
Find,FindAll, andRemoveAlluse predicates heavily. - '
Func<T, bool>often fills the same role, especially with LINQ.' - Keep predicates readable and preferably side-effect free.

