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

定義

判斷陣列中的每一個項目是否符合指定之述詞所定義的條件。

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

陣列項目的類型。

參數

array
T[]

要檢查之以零為起始的一維 Array 所根據的條件。

match
Predicate<T>

述詞,可定義檢查項目所根據的條件。

傳回

如果 array 中的每一個項目都符合指定之述詞所定義的條件,則為 true,否則為 false。 如果陣列中沒有項目,則傳回值為 true

例外狀況

arraynull

-或-

matchnull

範例

下列範例會判斷字串陣列中每個元素的最後一個字元是否為數字。 它會建立兩個字串陣列。 第一個陣列包含以字母字元結尾的字串,以及以數值字元結尾的字串。 第二個數組只包含以數值字元結尾的字串。 此範例也會定義簽 EndWithANumber 章符合 Predicate<T> 委派的方法。 此範例會將每個陣列傳遞至 TrueForAll 方法,以及代表 方法的 EndsWithANumber 委派。

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 int.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.
open System

let endsWithANumber (value: string) =
    value.Substring(value.Length - 1)
    |> Int32.TryParse
    |> fst

let values1 = [| "Y2K"; "A2000"; "DC2A6"; "MMXIV"; "0C3" |]
let values2 = [| "Y2"; "A2000"; "DC2A6"; "MMXIV_0"; "0C3" |]

if Array.TrueForAll(values1, endsWithANumber) then
    printfn "All elements end with an integer."
else
    printfn "Not all elements end with an integer."

if Array.TrueForAll(values2, endsWithANumber) then
    printfn "All elements end with an integer."
else
    printfn "Not all elements end with an integer."


// 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 運算式。

using System;

public class Example
{
   public static void Main()
   {
      String[] values = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };
      if (Array.TrueForAll(values, value => {
                                      int s;
                                      return int.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.
open System

let values = [| "Y2K"; "A2000"; "DC2A6"; "MMXIV"; "0C3" |]
if Array.TrueForAll(values, 
    fun value -> 
        value.Substring(value.Length - 1) 
        |> Int32.TryParse 
        |> fst) then
    printfn "All elements end with an integer."
else
    printfn "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 。 否則,它會在逐一查看陣列中的所有專案之後傳回 true

注意

如 C# 和 Visual Basic 所示,不需要在 Visual Basic 中明確建立 Predicate<string> 委派 Predicate(Of String) () 。 這些語言會從內容推斷正確的委派,並自動建立它。

備註

Predicate<T>是方法的委派,如果傳遞至該方法的物件符合委派中定義的條件,則會傳回 true 。 的元素 array 會個別傳遞至 Predicate<T> ,而且當委派傳回 false 任何元素時,會停止處理。

這個方法是 O (n) 作業,其中 nLengtharray

適用於

另請參閱