
Array.TrueForAll is a static Array method. It gives you a declarative way to test every element in your array for some condition using a Predicate. And it returns true or false.
This C# example program uses the Array.TrueForAll method. TrueForAll tests elements with a Predicate.

First, this example code demonstrates the Array.TrueForAll method in two contexts. The first test uses an integer array of four odd numbers. The Array.TrueForAll method is invoked and the predicate is specified: it returns true whenever a number is not evenly divisible by two. For more information on the syntax used, please see the lambda expression article.
Lambda ExpressionProgram that uses Array.TrueForAll [C#]
using System;
class Program
{
static void Main()
{
{
int[] values = { 1, 3, 5, 7 };
bool result = Array.TrueForAll(values, y => y % 2 == 1);
Console.WriteLine(result);
}
{
int[] values = { 2, 5, 8 };
bool result = Array.TrueForAll(values, y => y % 2 == 1);
Console.WriteLine(result);
}
}
}
Output
True
FalseSecond part. In the second part, the numbers in the array are not all odd; therefore, the same Array.TrueForAll parameters will have the method return false. In this way, we can see that Array.TrueForAll correctly applies the predicate to every element in the array.

Obviously, you could imperatively code a method that does the same thing as Array.TrueForAll: simply test each element in foreach-loop and return early if an element doesn't match. Typically, this sort of approach is much faster than using a call such as Array.TrueForAll; therefore, in performance work, using a foreach or for loop is better. On the other hand, Array.TrueForAll can yield more concise code in some cases.

We examined the Array.TrueForAll method in the C# programming language and .NET Framework. By applying the predicate argument to each element in the array argument, you can use it to test every element of the array for some condition without using a loop construct at all.
Array Types