Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between All and Any in C# Linq?
The Any() and All() methods are LINQ extension methods that return bool values based on predicate conditions. Any() returns true if at least one element matches the predicate, while All() returns true only if every element matches the predicate.
Both methods provide overloads − one that accepts a predicate and another parameterless version that simply checks if the collection contains any elements.
Syntax
Following is the syntax for Any() method −
bool result = collection.Any(); bool result = collection.Any(predicate);
Following is the syntax for All() method −
bool result = collection.All(predicate);
Key Differences
| Any() | All() |
|---|---|
Returns true if at least one element matches the predicate |
Returns true only if every element matches the predicate |
| Stops execution once the first matching element is found | Stops execution once the first non-matching element is found |
| Has a parameterless overload to check if collection is non-empty | No parameterless overload − requires a predicate |
Returns true for empty collections (parameterless version) |
Returns true for empty collections with any predicate |
Using Any() Method
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
IEnumerable<double> doubles = new List<double> { 1.2, 1.7, 2.5, 2.4 };
bool result = doubles.Any(val => val < 1);
Console.WriteLine("Any value less than 1: " + result);
IEnumerable<double> doubles1 = new List<double> { 0.8, 1.7, 2.5, 2.4 };
bool result1 = doubles1.Any(val => val < 1);
Console.WriteLine("Any value less than 1: " + result1);
// Parameterless version - checks if collection is non-empty
List<int> emptyList = new List<int>();
Console.WriteLine("Empty list has any elements: " + emptyList.Any());
}
}
The output of the above code is −
Any value less than 1: False Any value less than 1: True Empty list has any elements: False
Using All() Method
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
IEnumerable<double> doubles = new List<double> { 0.8, 0.9, 0.6, 0.7 };
bool result = doubles.All(val => val < 1);
Console.WriteLine("All values less than 1: " + result);
IEnumerable<double> doubles1 = new List<double> { 0.8, 0.9, 1.0, 0.7 };
bool result1 = doubles1.All(val => val < 1);
Console.WriteLine("All values less than 1: " + result1);
// All() with empty collection returns true
List<int> emptyList = new List<int>();
Console.WriteLine("All elements in empty list are positive: " + emptyList.All(x => x > 0));
}
}
The output of the above code is −
All values less than 1: True All values less than 1: False All elements in empty list are positive: True
Practical Example with String Collections
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" };
// Any() - Check if any name starts with 'A'
bool anyStartsWithA = names.Any(name => name.StartsWith("A"));
Console.WriteLine("Any name starts with A: " + anyStartsWithA);
// All() - Check if all names have length greater than 2
bool allLongerThan2 = names.All(name => name.Length > 2);
Console.WriteLine("All names longer than 2 characters: " + allLongerThan2);
// All() - Check if all names start with uppercase
bool allStartWithUpper = names.All(name => char.IsUpper(name[0]));
Console.WriteLine("All names start with uppercase: " + allStartWithUpper);
}
}
The output of the above code is −
Any name starts with A: True All names longer than 2 characters: True All names start with uppercase: True
Conclusion
Any() and All() are efficient LINQ methods for condition checking in collections. Use Any() when you need to verify if at least one element meets a condition, and All() when every element must satisfy the condition. Both methods use short-circuit evaluation for optimal performance.
