📜  C#| Array.TrueForAll()方法

📅  最后修改于: 2021-05-29 20:07:33             🧑  作者: Mango

此方法用于确定数组中的每个元素是否与指定谓词定义的条件匹配。

句法:

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

在此,T是数组元素的类型。

参数:

返回值:如果数组中的每个元素都匹配指定谓词定义的条件,则此方法返回true,否则返回false。如果数组中没有元素,则返回值为true

异常:如果数组为nullmatch为null,则此方法将引发ArgumentNullException。

下面的程序说明了Array.TrueForAll(T [],Predicate的用法) 方法

范例1:

// C# program to demonstrate
// TrueForAll() method
using System;
using System.Collections.Generic;
  
public class GFG {
  
// Main Method
public static void Main()
{
  
    try {
  
        // Creating and intializing new the String
        String[] myArr = {"Sun", "Son", "Sue", "Shu"};
  
        // Display the values of the myArr.
        Console.WriteLine("Initial Array:");
  
        // calling the PrintIndexAndValues()
        // method to print
        PrintIndexAndValues(myArr);
  
        // getting the bool value 
        // with required condition
        // using method TrueForAll()
        bool value = Array.TrueForAll(myArr, element => element.StartsWith("S",
                                                    StringComparison.Ordinal));
  
        // Checking the condition
        if (value)
            Console.Write("Every Element is satisfying condition");
        else
            Console.Write("Every Element is not satisfying condition");
    }
    catch (ArgumentException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
  
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
    for (int i = 0; i < myArr.Length; i++) {
  
        Console.WriteLine("{0}", myArr[i]);
    }
    Console.WriteLine();
}
}
输出:
Initial Array:
Sun
Son
Sue
Shu

Every Element is satisfying condition

示例2:对于ArgumentNullException

// C# program to demonstrate
// TrueForAll() method
// For ArgumentNullException
using System;
using System.Collections.Generic;
  
public class GFG {
  
// Main Method
public static void Main()
{
  
    try {
  
        // Creating and initializing new 
        // the String with a null value
        String[] myArr = null;
  
        // getting the bool value 
        // with required condition
        // using method TrueForAll()
        Console.WriteLine("Trying to get the boolean "
                        +"value while myArr is null");
        Console.WriteLine();
        bool value = Array.TrueForAll(myArr, element => element.StartsWith("S",
                                                    StringComparison.Ordinal));
  
        // Checking the condition
        if (value)
            Console.Write("Every Element is satisfying condition");
        else
            Console.Write("Every Element is not satisfying condition");
    }
    catch (ArgumentException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
  
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
    for (int i = 0; i < myArr.Length; i++) {
  
        Console.WriteLine("{0}", myArr[i]);
    }
    Console.WriteLine();
}
}
输出:
Trying to get the boolean value while myArr is null

Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.trueforall?view=netframework-4.7.2