C#
predicate
programming
duplicates
.NET

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:

csharp
bool SomeCheck(T value)

That means the method receives one value and returns either true or false.

Here is a simple example:

csharp
1using System;
2
3Predicate<int> isEven = x => x % 2 == 0;
4
5Console.WriteLine(isEven(4)); // True
6Console.WriteLine(isEven(5)); // False

The predicate does not change the input. It only evaluates it.

Where Predicates Are Used

Predicates show up in many .NET collection methods.

csharp
1using System;
2using System.Collections.Generic;
3
4var numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
5
6int firstEven = numbers.Find(x => x % 2 == 0);
7List<int> evens = numbers.FindAll(x => x % 2 == 0);
8numbers.RemoveAll(x => x < 3);
9
10Console.WriteLine(firstEven);
11Console.WriteLine(string.Join(", ", evens));
12Console.WriteLine(string.Join(", ", numbers));

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>:

csharp
1using System;
2
3Func<int, bool> isPositive = x => x > 0;
4Console.WriteLine(isPositive(10));

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.

csharp
1using System;
2using System.Linq;
3
4var words = new[] { "apple", "pear", "banana", "kiwi" };
5var longWords = words.Where(word => word.Length >= 5);
6
7Console.WriteLine(string.Join(", ", longWords));

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.

csharp
1using System;
2using System.Collections.Generic;
3
4static bool IsAdult(int age) => age >= 18;
5
6var ages = new List<int> { 12, 18, 25, 15 };
7var adults = ages.FindAll(IsAdult);
8
9Console.WriteLine(string.Join(", ", adults));

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:

csharp
Predicate<string> hasValue = s => !string.IsNullOrWhiteSpace(s);

Harder to maintain:

csharp
1Predicate<Order> rule = order =>
2    order != null &&
3    order.IsPaid &&
4    order.Items.Count > 0 &&
5    order.Customer != null &&
6    order.Customer.IsActive &&
7    order.Total >= 100;

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:

csharp
1int calls = 0;
2Predicate<int> bad = x =>
3{
4    calls++;
5    return x % 2 == 0;
6};

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, and RemoveAll use predicates heavily.
  • 'Func<T, bool> often fills the same role, especially with LINQ.'
  • Keep predicates readable and preferably side-effect free.

Course illustration
Course illustration

All Rights Reserved.