In C#, List.TrueForAll(match) asks: do all the items pass my test? The test is a predicate lambda like n => n > 0, which reads "for each n, is n greater than 0?".
The method stops at the first item that fails and returns false. If it gets to the end without a failure, it returns true. This makes it the opposite partner of Exists(), which asks whether any item passes.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.TrueForAll(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda that returns true for good items. You get one bool for the whole list.
C#
List<int> scores = new List<int> { 70, 85, 90 };
bool allPass = scores.TrueForAll(s => s >= 50); // true
bool allHigh = scores.TrueForAll(s => s >= 80); // false
Call
What it does
scores.TrueForAll(s => s >= 50)
true only if every score is 50 or more.
names.TrueForAll(n => n.Length > 0)
true only if no name is empty.
list.TrueForAll(x => false)
false for a list with items, but true for an empty list.
An empty list returns true. No item fails, so the claim "all items pass" holds. This is a common surprise, so check Count > 0 too when an empty list should count as a failure. A null predicate throws ArgumentNullException. LINQ's All(predicate) works the same way, and Array.TrueForAll is the array version.
Library declarations
C#
public bool TrueForAll(Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that takes one item and returns true if it is acceptable. It must not be null.
Return Value
Return Value
Description
bool
true if every item passes, or if the list is empty. false as soon as one item fails.
C# Examples
Example 1 — Do All Scores Pass?
Test one list with a loose rule and a strict rule.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> scores = new List<int> { 70, 85, 90 };
Console.WriteLine(scores.TrueForAll(s => s >= 50));
Console.WriteLine(scores.TrueForAll(s => s >= 80));
Console.WriteLine(scores.TrueForAll(s => s <= 100));
}
}
Output
True
False
True
How It Works
Loose rule.70, 85, and 90 are all at least 50, so the scan reaches the end and returns True.
Strict rule stops early.70 is below 80, so the test fails on the first item. The method returns False without checking the others.
Upper limit. All three scores are 100 or less, so the third line is True.
One bad item is enough. A single failing item decides the answer. It does not matter how many items pass.
Example 2 — The Empty List Surprise
An empty list has no failing item, so TrueForAll says true. Add a Count check when that is not what you want.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> empty = new List<int>();
List<int> one = new List<int> { 5 };
Console.WriteLine(empty.TrueForAll(n => n > 100));
Console.WriteLine(one.TrueForAll(n => n > 100));
bool strict = empty.Count > 0 && empty.TrueForAll(n => n > 100);
Console.WriteLine(strict);
}
}
Output
True
False
False
How It Works
Nothing to fail. The empty list has zero items, so no item can break the rule. The answer is True.
One item fails. The list { 5 } has an item below 100, so it returns False.
Extra guard.empty.Count > 0 is False, so the whole && expression is False and the lambda is never called.
Pick the meaning you need. If "all items pass" must imply "there is at least one item", always write the guard.
Example 3 — TrueForAll and Exists Are Opposites
"All pass" means the same as "none fail". Show that TrueForAll(test) equals !Exists(not test).
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "", "Chen" };
bool allNamed = names.TrueForAll(s => s.Length > 0);
bool anyBlank = names.Exists(s => s.Length == 0);
Console.WriteLine(allNamed);
Console.WriteLine(anyBlank);
Console.WriteLine(allNamed == !anyBlank);
}
}
Output
False
True
True
How It Works
All have letters? The third item is an empty string, so the test Length > 0 fails there, and allNamed is False.
Any blank? The opposite question, Length == 0, finds that same empty string, so anyBlank is True.
Two views of one fact. "Not everything is non-empty" and "something is empty" are the same statement, so allNamed equals !anyBlank.
Pick the clearer one. Use TrueForAll when you validate that all items are fine, and Exists when you look for one problem item.
Frequently Asked Questions
It returns true if every item in the list passes your test lambda, and false as soon as one item fails.
true, because no item fails the test. Add a Count > 0 check if an empty list should not count as success.
TrueForAll needs every item to pass. Exists needs at least one item to pass.
Yes, the result is the same. TrueForAll is a List<T> and Array method. All(predicate) comes from System.Linq and works on any collection.
Not always. It stops at the first item that fails, because the answer is then already false.