C# LINQ Any and All Operators Explained

In C# LINQ, Any and All are quantifier operators used to validate collections against specified conditions by returning a boolean result. While Any checks whether at least one element in a sequence meets a condition (or simply checks if a collection contains any elements), All verifies whether every single element satisfies a given predicate. This article provides a clear breakdown of how both operators function, their performance advantages such as short-circuit evaluation, and practical code examples demonstrating their use.

The LINQ Any Operator

The Any operator determines whether any element in a collection matches a provided condition, or whether a collection contains any elements at all. It returns true if it finds at least one matching element; otherwise, it returns false.

Any has two primary overloads:

  1. Parameterless Any(): Returns true if the collection contains at least one item, regardless of its value. This is more efficient than checking collection.Count() > 0 on IEnumerable<T> because it does not need to enumerate the entire collection.
  2. Predicate-based Any(predicate): Evaluates each element against a specified condition until a match is found.
List<int> numbers = new List<int> { 3, 5, 8, 9 };

// Checks if the list has any elements
bool hasElements = numbers.Any(); // Returns true

// Checks if any element is even
bool hasEvenNumber = numbers.Any(n => n % 2 == 0); // Returns true

The LINQ All Operator

The All operator checks whether all elements in a collection satisfy a specified condition. It returns true only if every element matches the criteria; otherwise, it returns false. Unlike Any, the All operator requires a predicate.

List<int> numbers = new List<int> { 2, 4, 6, 8 };

// Checks if all elements are even
bool allEven = numbers.All(n => n % 2 == 0); // Returns true

// Checks if all elements are greater than 5
bool allGreaterThanFive = numbers.All(n => n > 5); // Returns false

Short-Circuit Evaluation

Both Any and All utilize short-circuit evaluation to optimize performance:

  • Any short-circuits on true: As soon as it encounters the first element that satisfies the predicate, it stops enumerating the sequence and immediately returns true.
  • All short-circuits on false: As soon as it encounters the first element that fails the predicate, it stops enumerating and immediately returns false.

This behavior makes both operators highly efficient on large collections or unbounded sequences, as they rarely need to process the entire collection unless the condition demands it.

Behavior on Empty Collections

Understanding how these operators handle empty sequences is essential to prevent logic bugs:

  • Any() on an empty collection always returns false because there are no elements to satisfy the condition.
  • All() on an empty collection always returns true. This follows the mathematical principle of vacuous truth: because there are zero elements in the collection, there is no element that violates the condition.