C# 数组中的 TrueForAll() 方法

csharpprogrammingserver side programming更新于 2025/9/22 22:37:17

使用数组中的 TrueForAll() 方法,您可以检查每个元素是否符合条件。

让我们看一个例子 −

示例

using System;
using System.Text;
public class Demo {
   public static void Main() {
      int[] val = { 97, 45, 76, 21, 89, 45 };
      // 检查所有数组元素是否大于 1
      bool result = Array.TrueForAll(val, res => res > 1);
      Console.WriteLine(result);
   }
}

输出

True

使用数组中的 TrueForAll() 方法,您可以检查每个元素是否符合条件。

让我们看一个例子 −

示例

using System;
using System.Text;
public class Demo {
   public static void Main() {
      int[] val = { 97, 45, 76, 21, 89, 45 };
      // 检查所有数组元素是否多于一个
      bool result = Array.TrueForAll(val, res => res > 1);
      Console.WriteLine(result);
   }
}

输出

True

相关文章