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>

用于定义检查元素时要对照的条件的谓词。

返回

Boolean

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

例外

arraynull

  • 或 -

match 上声明的默认值为 null

示例

以下示例确定字符串数组中每个元素的最后一个字符是否为数字。 它创建两个字符串数组。 第一个数组包括以字母字符结尾的字符串和以数字字符结尾的字符串。 第二个数组仅包含以数字字符结尾的字符串。 该示例还定义了一个 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该方法。 单个传递给Predicate<T>元素的元素array,当委托返回false任何元素时,将停止处理。

此方法是 O (n) 操作,其位置 nLength array.

适用于

另请参阅