In C#, Array.TrueForAll checks whether every element passes your predicate. If all do, it returns true. If any element fails, it returns false.
Think of it as the opposite style of question from Exists: “are all items OK?” versus “is any item a problem?”
This is a static member of System.Array. You call it with the class name, such as Array.TrueForAll(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Pass the array and predicate; use the bool in validation logic.
C#
bool allPos = Array.TrueForAll(nums, x => x > 0);
Call
What it does
Array.TrueForAll(nums, x => x > 0)
True only if every int is positive.
Array.TrueForAll(names, n => n.Length > 0)
True only if no name is empty.
On an empty array, TrueForAll returns true (there is no counterexample).
Library declarations
C#
public static bool TrueForAll<T>(T[] array, Predicate<T> match);
Parameters
Parameter
Description
array
Required. Array to test.
match
Required. Predicate each element must satisfy.
Return Value
Return Value
Description
true
Every element matched (or the array is empty).
false
At least one element failed.
C# Examples
Example 1 — All Positive?
Validate that every number is greater than zero.
C#
using System;
class Program {
static void Main() {
int[] nums = { 1, 4, 9 };
Console.WriteLine(Array.TrueForAll(nums, x => x > 0));
}
}
Output
True
How It Works
All pass. 1, 4, and 9 are positive.
Validation style. Useful before accepting user data.
Bool only. You do not learn which index failed.
Pair with FindIndex. Use FindIndex to locate the first bad value.
Example 2 — False When One Fails
A single zero breaks an all-positive check.
C#
using System;
class Program {
static void Main() {
int[] nums = { 1, 0, 3 };
Console.WriteLine(Array.TrueForAll(nums, x => x > 0));
}
}
Output
False
How It Works
Zero fails.x > 0 is false at index 1.
Whole result false. TrueForAll requires every element.
Can stop early. After a failure, remaining elements need not be tested.
Exists contrast. Exists(x => x == 0) would be true here.
Example 3 — Empty Array Is True
No elements means no counterexamples.
C#
using System;
class Program {
static void Main() {
int[] none = Array.Empty<int>();
Console.WriteLine(Array.TrueForAll(none, x => x > 0));
}
}
Output
True
How It Works
Vacuous truth. With zero elements, the “all” claim holds.
Know your rules. Some apps should treat empty as invalid separately.
Length check. Combine with array.Length > 0 when needed.
Same as LINQ All. The idea matches All on empty sequences.
Frequently Asked Questions
true if every element matches the predicate; otherwise false.
TrueForAll returns true because no element failed the test.
Exists is “any match?”. TrueForAll is “all match?”.
Use FindIndex with the opposite condition, or FindIndex with x => !rule(x).