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

조건에 대해 확인할 1차원 Array (인덱스는 0부터 시작)입니다.

match
Predicate<T>

요소에 대해 확인할 조건을 정의하는 조건자입니다.

반환

Boolean

array의 모든 요소가 지정한 조건자에 정의된 조건과 일치하면 true이고, 그렇지 않으면 false입니다. 배열에 요소가 없으면 반환 값은 true입니다.

예외

array이(가) null인 경우

또는

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 요소가 숫자의 문자열 표현으로 끝나는지 여부를 결정하는 람다 식과 함께 문자열 배열을 메서드에 전달한다는 점을 제외하고 첫 번째 예제와 비슷합니다.

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 대리자를 명시적으로 만들 Predicate<string> 필요가 없습니다(Predicate(Of String)Visual Basic). 이러한 언어는 컨텍스트에서 올바른 대리자를 유추하고 자동으로 만듭니다.

설명

전달 Predicate<T> 된 개체가 대리자에서 정의된 조건과 일치하는지 반환true 하는 메서드에 대한 대리자입니다. 요소는 array 개별적으로 전달되며 대리자가 모든 요소에 Predicate<T>대해 반환 false 될 때 처리가 중지됩니다.

이 메서드는 O(n) 연산입니다. 여기서 nLength array.

적용 대상

추가 정보