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>

定義條件以與元素進行檢驗的謂詞。

傳回

true 若 中的 array 每個元素都符合指定謂詞所定義的條件;否則, 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.

以下範例與第一個類似,但會將字串陣列與一個 lambda 運算式一併傳給方法, TrueForAll 該表達式判斷某個陣列元素是否以數字的字串表示結尾。

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

Note

如同兩個例子所示,在 C# 和 Visual Basic 中,不需要明確建立 Predicate<string>代理(Predicate(Of String),Visual Basic中為此)。 這些語言會從上下文推斷正確的代理並自動建立。

備註

Predicate<T> 方法的代理,若傳遞給該方法的物件符合代理中定義的條件,該代理會回傳true 。 的 array 元素會分別傳遞給 Predicate<T>,當代理返回 false 任何元素時,處理即停止。

此方法為 O(n) 運算,其中 nLengtharray

適用於

另請參閱