C# Array TrueForAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

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);
CallWhat 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

ParameterDescription
arrayRequired. Array to test.
matchRequired. Predicate each element must satisfy.

Return Value

Return ValueDescription
trueEvery element matched (or the array is empty).
falseAt 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));
    }
}

How It Works

  1. All pass. 1, 4, and 9 are positive.
  2. Validation style. Useful before accepting user data.
  3. Bool only. You do not learn which index failed.
  4. 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));
    }
}

How It Works

  1. Zero fails. x > 0 is false at index 1.
  2. Whole result false. TrueForAll requires every element.
  3. Can stop early. After a failure, remaining elements need not be tested.
  4. 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));
    }
}

How It Works

  1. Vacuous truth. With zero elements, the “all” claim holds.
  2. Know your rules. Some apps should treat empty as invalid separately.
  3. Length check. Combine with array.Length > 0 when needed.
  4. 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).
No.

Back to the method index

See all 32 System.Array method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful