Array.TrueForAll(T[], Predicate<T>) Array.TrueForAll(T[], Predicate<T>) Array.TrueForAll(T[], Predicate<T>) Array.TrueForAll(T[], Predicate<T>) Method

定義

判斷陣列中的每一個項目是否符合指定之述詞所定義的條件。Determines whether every element in the array matches the conditions defined by the specified predicate.

public:
generic <typename T>
 static bool TrueForAll(cli::array <T> ^ array, Predicate<T> ^ match);
public static bool TrueForAll<T> (T[] array, Predicate<T> match);
static member TrueForAll : 'T[] * Predicate<'T> -> bool
Public Shared Function TrueForAll(Of T) (array As T(), match As Predicate(Of T)) As Boolean

類型參數

T

陣列項目的類型。The type of the elements of the array.

參數

array
T[]

要檢查之以零為起始的一維 Array 所根據的條件。The one-dimensional, zero-based Array to check against the conditions.

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

述詞,可定義檢查項目所根據的條件。The predicate that defines the conditions to check against the elements.

傳回

如果 array 中的每一個項目都符合指定之述詞所定義的條件,則為 true,否則為 falsetrue if every element in array matches the conditions defined by the specified predicate; otherwise, false. 如果陣列中沒有項目,則傳回值為 trueIf there are no elements in the array, the return value is true.

例外狀況

arraynullarray is null.

-或--or- matchnullmatch is null.

範例

下列範例會判斷每個項目的字串陣列中的最後一個字元是否是數字。The following example determines whether the last character of each element in a string array is a number. 它會建立兩個字串陣列。It creates two string arrays. 第一個陣列包含兩個字母字元的結尾的字串和數字字元的結尾的字串。第二個陣列只包含數字字元的結尾的字串。The first array includes both strings that end with alphabetic characters and strings that end with numeric characters.The second array consists only of strings that end with numeric characters. 此範例也會定義EndWithANumber方法的簽章符合Predicate<T>委派。The example also defines an EndWithANumber method whose signature matches the Predicate<T> delegate. 此範例會傳遞至每個陣列TrueForAll方法,以及委派,表示EndsWithANumber方法。The example passes each array to the TrueForAll method along with a delegate that represents the EndsWithANumber method.

using System;

public class Example
{
   public static void Main()
   {
      String[] values1 = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };
      String[] values2 = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" };


      if (Array.TrueForAll(values1, EndsWithANumber))
         Console.WriteLine("All elements end with an integer.");
      else
         Console.WriteLine("Not all elements end with an integer.");
       
      if (Array.TrueForAll(values2, 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 Int32.TryParse(value.Substring(value.Length - 1), out s);
   }
}
// The example displays the following output:
//       Not all elements end with an integer.
//       All elements end with an integer.
Module Example
   Public Sub Main()
      Dim values1() As String = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" }
      Dim values2() As String = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" }


      If Array.TrueForAll(values1, AddressOf EndsWithANumber) Then
         Console.WriteLine("All elements end with an integer.")
      Else
         Console.WriteLine("Not all elements end with an integer.")
      End If  
       
      If Array.TrueForAll(values2, AddressOf EndsWithANumber) Then
         Console.WriteLine("All elements end with an integer.")
      Else
         Console.WriteLine("Not all elements end with an integer.")
      End If   
   End Sub

   Private Function EndsWithANumber(value As String) As Boolean
      Dim s As Integer
      Return Int32.TryParse(value.Substring(value.Length - 1), s)
   End Function
End Module
' The example displays the following output:
'       Not all elements end with an integer.
'       All elements end with an integer.

下列範例是類似於第一天,不同之處在於它會傳遞至字串陣列TrueForAll以及判斷特定的陣列項目是否結束的數字的字串表示的 lambda 運算式的方法。The following example is similar to the first, except that it passes the string array to the TrueForAll method along with a lambda expression that determines whether a particular array element ends with the string representation of a number.

using System;

public class Example
{
   public static void Main()
   {
      String[] values = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };
      if (Array.TrueForAll(values, value => {
                                      int s; 
                                      return Int32.TryParse(value.Substring(value.Length - 1), out s); }
                                   ))
         Console.WriteLine("All elements end with an integer.");
      else
         Console.WriteLine("Not all elements end with an integer.");
   }
}
// The example displays the following output:
//        Not all elements end with an integer.
Module Example
   Public Sub Main()
      Dim values() As String = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" }
      'Dim values2() As String = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" }

      If Array.TrueForAll(values, Function(value) 
                                     Dim s As Integer
                                     Return Int32.TryParse(value.Substring(value.Length - 1), s)
                                  End Function) Then
         Console.WriteLine("All elements end with an integer.")
      Else
         Console.WriteLine("Not all elements end with an integer.")
      End If   
   End Sub
End Module
' The example displays the following output:
'       Not all elements end with an integer.

在這兩種情況下,TrueForAll方法會傳回false當它遇到結尾不是數字的第一個陣列元素。In both cases, the TrueForAll method returns false as soon as it encounters the first array element that does not end in a number. 否則,它會傳回true後逐一查看陣列中的所有項目。Otherwise, it returns true after iterating all the elements in the array.

注意

如這兩個範例所示,在 C# 和 Visual Basic 中,不需要建立Predicate<string>委派 (Predicate(Of String) Visual Basic 中) 明確。As both examples show, in C# and Visual Basic, it is not necessary to create the Predicate<string> delegate (Predicate(Of String) in Visual Basic) explicitly. 這些語言推斷正確的委派,從內容,並自動建立。These languages infer the correct delegate from context and create it automatically.

備註

Predicate<T>會傳回方法的委派true物件傳遞給是否符合委派中定義的條件。The Predicate<T> is a delegate to a method that returnstrue if the object passed to it matches the conditions defined in the delegate. 項目array個別傳遞給Predicate<T>,並在委派傳回時,會停止處理false的任何項目。The elements of array are individually passed to the Predicate<T>, and processing is stopped when the delegate returns false for any element.

這個方法是 O (n) 運算,其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

適用於

另請參閱