C# Array - TrueForAll() Method



The C# Array TrueForAll() method is used to determine whether all elements in the array match the conditions defined by the specified predicate. Return true if every element in the array matches the condition defined by the specified predicate; otherwise, return false.

If there is no element in the array, this function return true.

Syntax

Following is the syntax of the C# Array TrueForAll() method −

public static bool TrueForAll<T> (T[] array, Predicate<T> match);

Parameters

This method accepts the following parameters −

  • array: An one-dimensional array to check against the condition.
  • match: It represents the predicate that defines the conditions to check against the elements.

Return value

This method returns boolean value. True if every element matches specified predicate; otherwise, false.

Example 1: Check Positive Integer

Let us crate a basic example of the TrueForAll() method to check whether every element in the array is positive −

using System;
public class Example {
   public static void Main() {
      // Integers Array
      int[] numbers = { 5, 10, 15, 20, 25 };

      // Using Array.TrueForAll to check if all elements are positive
      bool allPositive = Array.TrueForAll(numbers, IsPositive);

      if (allPositive)
         Console.WriteLine("All elements in the array are positive.");
      else
         Console.WriteLine("Not all elements in the array are positive.");
   }
   public static bool IsPositive(int number) {
      return number > 0;
   }
}

Output

Following is the output −

All elements in the array are positive.

Example 2: Check Every Element ends with an Integer

Let us see another example of the TrueForAll() method to check whether every elements ends with an integer or not −

using System;
public class Example {
   public static void Main() {
      String[] values = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };

      if (Array.TrueForAll(values, EndsWithANumber))
         Console.WriteLine("All elements end with an integer.");
      else
         Console.WriteLine("Not all elements end with an integer.");
   }

   private static bool EndsWithANumber(string value){
      int s;
      return int.TryParse(value.Substring(value.Length - 1), out s);
   }
}

Output

Following is the output −

Not all elements end with an integer.

Example 3: String Ending with X

This is another, example of the TrueForAll() method. Here, we use this method to check whether every element ends with x or not −

using System;

public class Example {
   public static void Main() {
      // String Array
      string[] words = { "Box", "Fox", "Mix", "Six" };

      // Check if all words end with 'X'
      bool allEndWithX = Array.TrueForAll(words, EndsWithX);

      if (allEndWithX)
         Console.WriteLine("All words in the array end with 'X'.");
      else
         Console.WriteLine("Not all words in the array end with 'X'.");
   }

   // Predicate method to check if a string ends with 'X'
   public static bool EndsWithX(string word) {
      if (string.IsNullOrEmpty(word))
         return false;

      return word.EndsWith("X", StringComparison.OrdinalIgnoreCase);
   }
}

Output

Following is the output −

All words in the array end with 'X'.

Example 4: Check All Elements are Even Number

Here, in this example we uses the TrueForAll() method to check whether every element is even or not in the array −

using System;
public class Example {
   public static void Main() {
      // Integer Array
      int[] numbers = { 2, 4, 6, 8, 10 };

      // Check if all elements are even numbers
      bool allEven = Array.TrueForAll(numbers, IsEven);

      if (allEven)
         Console.WriteLine("All elements in the array are even numbers.");
      else
         Console.WriteLine("Not all elements in the array are even numbers.");
   }

   // Predicate method to check if a number is even
   public static bool IsEven(int number) {
      return number % 2 == 0;
      return number % 2 == 0;
   }
}

Output

Following is the output −

All elements in the array are even numbers.
csharp_array_class.htm
Advertisements