Enumerable.Any() / All() / Contains()
| Since: | C# 3.0(2007) |
|---|
LINQ extension methods for checking conditions or the presence of elements in a sequence.
Syntax
using System.Linq; // Whether at least one element exists (returns false for an empty sequence) bool any = source.Any(); bool any = source.Any(x => condition); // Whether all elements satisfy the condition (returns true for an empty sequence) bool all = source.All(x => condition); // Whether the sequence contains the specified value bool has = source.Contains(value); bool has = source.Contains(value, IEqualityComparer<T>);
Method List
| Method | Description |
|---|---|
| Any() | Returns true if the sequence contains at least one element. |
| Any(predicate) | Returns true if at least one element satisfies the condition. |
| All(predicate) | Returns true if all elements satisfy the condition. Always returns true for an empty sequence. |
| Contains(value) | Returns true if the specified value is found in the sequence. |
| Contains(value, comparer) | Checks whether a value exists using a custom equality comparer. |
| SequenceEqual(second) | Returns true if two sequences are equal in both order and elements. |
Sample Code
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
List<int> scores = new List<int> { 80, 95, 70, 88, 60, 92 };
Console.WriteLine(scores.Any()); // true (sequence is not empty)
Console.WriteLine(scores.Any(s => s >= 90)); // true (a score of 90 or higher exists)
Console.WriteLine(scores.Any(s => s > 100)); // false (no score exceeds 100)
Console.WriteLine(scores.All(s => s >= 60)); // true (all scores are 60 or higher)
Console.WriteLine(scores.All(s => s >= 80)); // false (some scores are below 80)
Console.WriteLine(scores.Contains(88)); // true
Console.WriteLine(scores.Contains(100)); // false
// Behavior with an empty sequence
List<int> empty = new List<int>();
Console.WriteLine(empty.Any()); // false
Console.WriteLine(empty.All(n => n > 0)); // true (always true for empty sequences)
// Using Contains with a string list, ignoring case.
List<string> members = new List<string> { "Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi" };
bool hasGojo = members.Contains("gojo satoru", StringComparer.OrdinalIgnoreCase);
Console.WriteLine(hasGojo); // true
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = new List<int> { 1, 2, 3 };
List<int> c = new List<int> { 3, 2, 1 };
Console.WriteLine(a.SequenceEqual(b)); // true (same order and values)
Console.WriteLine(a.SequenceEqual(c)); // false (different order)
Run the following command:
dotnet run True True False True False True False False True True True False
Common Mistakes
Common Mistake: All() Always Returns true for an Empty Sequence
All() returns true for an empty sequence (vacuous truth). If you need to guard against empty sequences, use Any() first to confirm the sequence has elements.
using System; using System.Collections.Generic; using System.Linq; // NG: All() on an empty sequence always returns true List<int> empty = new List<int>(); Console.WriteLine(empty.All(n => n > 0)); // True (even though the sequence is empty)
The same logic can also be written as:
using System;
using System.Collections.Generic;
using System.Linq;
// OK: check with Any() before calling All()
List<int> empty = new List<int>();
if (empty.Any() && empty.All(n => n > 0))
{
Console.WriteLine("All elements satisfy the condition.");
}
else
{
Console.WriteLine("No elements, or some elements do not satisfy the condition.");
}
Run the following command:
dotnet run True No elements, or some elements do not satisfy the condition.
Notes
Any() and All() use short-circuit evaluation. Any() stops iterating as soon as it finds an element that satisfies the condition, and All() stops as soon as it finds one that does not.
All() always returns true for an empty sequence (vacuous truth). If you need to guard against empty sequences, use Any() first to confirm the sequence has elements.
Using Any() is more efficient than checking element count for an emptiness check. For counting elements, see Enumerable.Count(). For filtering by a condition, see Enumerable.Where().
If you find any errors or copyright issues, please contact us.