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[]

要对照条件进行检查的从零开始的一维 ArrayThe 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.

注解

是一个方法委托, 如果传递给它的对象与委托中定义的条件相匹配, 则它将返回truePredicate<T>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.

此方法为 On() 运算, 其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

适用于

另请参阅