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
All Method in C#
The All() extension method is part of the System.Linq namespace. Using this method, you can check whether all the elements in a collection match a certain condition or not. It returns true if all elements satisfy the condition, otherwise false.
Syntax
Following is the syntax for the All() method −
bool result = collection.All(predicate);
Parameters
-
predicate − A function to test each element for a condition. It takes an element as input and returns a boolean value.
Return Value
The All() method returns true if all elements in the collection satisfy the condition specified by the predicate, or if the collection is empty. It returns false if any element does not satisfy the condition.
Using All() with Numeric Arrays
Example
The following example checks whether all elements in an array are greater than or equal to 2 −
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = { 6, 7, 15, 40, 55 };
bool res = arr.All(element => element >= 2);
Console.WriteLine("All elements >= 2: " + res);
// Check if all elements are even
bool allEven = arr.All(element => element % 2 == 0);
Console.WriteLine("All elements are even: " + allEven);
}
}
The output of the above code is −
All elements >= 2: True All elements are even: False
Using All() with String Collections
Example
The following example demonstrates using All() with string collections −
using System;
using System.Linq;
class Program {
static void Main() {
string[] names = { "Alice", "Bob", "Charlie", "David" };
// Check if all names have length >= 3
bool allLongNames = names.All(name => name.Length >= 3);
Console.WriteLine("All names have 3+ characters: " + allLongNames);
// Check if all names start with uppercase
bool allUppercase = names.All(name => char.IsUpper(name[0]));
Console.WriteLine("All names start with uppercase: " + allUppercase);
}
}
The output of the above code is −
All names have 3+ characters: True All names start with uppercase: True
Using All() with Empty Collections
Example
The All() method returns true for empty collections, as there are no elements to violate the condition −
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> emptyList = new List<int>();
List<int> nonEmptyList = new List<int> { 1, 3, 5 };
bool emptyResult = emptyList.All(x => x > 10);
bool nonEmptyResult = nonEmptyList.All(x => x > 10);
Console.WriteLine("Empty list - All > 10: " + emptyResult);
Console.WriteLine("Non-empty list - All > 10: " + nonEmptyResult);
}
}
The output of the above code is −
Empty list - All > 10: True Non-empty list - All > 10: False
Conclusion
The All() method in C# is a powerful LINQ extension that checks if all elements in a collection satisfy a given condition. It returns true when all elements match the predicate or when the collection is empty, making it useful for validation and filtering operations.
