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
C# Program to check whether the elements of a sequence satisfy a condition or not
The All() method in C# is used to check whether all elements in a sequence satisfy a specified condition. This method returns true only if every element meets the condition; if even one element fails the condition, it returns false.
The All() method is part of LINQ (Language Integrated Query) and can be used with arrays, lists, and other enumerable collections. It uses lambda expressions to define the condition that each element must satisfy.
Syntax
Following is the syntax for using the All() method −
bool result = collection.All(predicate);
Where predicate is a lambda expression that defines the condition −
collection.All(element => condition);
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()bool value −
true− if all elements satisfy the conditionfalse− if any element fails to satisfy the condition
Using All() with Numeric Conditions
This example checks if all elements in an array are greater than 20 −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] myArr = {7, 15, 22, 30, 40};
// checking if all the array elements are greater than 20
bool res = myArr.All(val => val > 20);
Console.WriteLine("All elements > 20: " + res);
// checking if all elements are positive
bool allPositive = myArr.All(val => val > 0);
Console.WriteLine("All elements positive: " + allPositive);
}
}
The output of the above code is −
All elements > 20: False All elements positive: True
Using All() with String Collections
The All() method works with any type of collection, including strings −
using System;
using System.Linq;
class StringExample {
static void Main() {
string[] names = {"Alice", "Bob", "Charlie", "David"};
// Check if all names have length greater than 3
bool allLongNames = names.All(name => name.Length > 3);
Console.WriteLine("All names have length > 3: " + allLongNames);
// Check if all names start with uppercase letter
bool allCapitalized = names.All(name => char.IsUpper(name[0]));
Console.WriteLine("All names capitalized: " + allCapitalized);
}
}
The output of the above code is −
All names have length > 3: True All names capitalized: True
Using All() with Complex Conditions
You can combine multiple conditions using logical operators −
using System;
using System.Linq;
class ComplexConditions {
static void Main() {
int[] numbers = {25, 30, 35, 40, 45};
// Check if all numbers are between 20 and 50
bool inRange = numbers.All(num => num >= 20 && num <= 50);
Console.WriteLine("All numbers between 20-50: " + inRange);
// Check if all numbers are even and greater than 20
bool evenAndGreater = numbers.All(num => num % 2 == 0 && num > 20);
Console.WriteLine("All even and > 20: " + evenAndGreater);
}
}
The output of the above code is −
All numbers between 20-50: True All even and > 20: False
Conclusion
The All() method is a powerful LINQ extension that checks if every element in a collection satisfies a given condition. It returns true only when all elements meet the criteria, making it useful for validation and filtering operations in C# applications.
