Array.Exists(T[], Predicate<T>) Array.Exists(T[], Predicate<T>) Array.Exists(T[], Predicate<T>) Array.Exists(T[], Predicate<T>) Method

定義

指定された配列に、指定された述語によって定義された条件と一致する要素が含まれているかどうかを判断します。Determines whether the specified array contains elements that match the conditions defined by the specified predicate.

public:
generic <typename T>
 static bool Exists(cli::array <T> ^ array, Predicate<T> ^ match);
public static bool Exists<T> (T[] array, Predicate<T> match);
static member Exists : 'T[] * Predicate<'T> -> bool
Public Shared Function Exists(Of T) (array As T(), match As Predicate(Of T)) As Boolean

型パラメーター

T

配列要素の型。The type of the elements of the array.

パラメーター

array
T[]

検索する 1 次元の Array。インデックス番号は 0 から始まります。The one-dimensional, zero-based Array to search.

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

検索する要素の条件を定義する Predicate<T>The Predicate<T> that defines the conditions of the elements to search for.

戻り値

指定された述語によって定義された条件と一致する 1 つ以上の要素が array に存在する場合は、true。それ以外の場合は falsetrue if array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.

例外

arraynullです。array is null.

- または --or- matchnullです。match is null.

次の例では、ラムダ式を使用して Exists メソッドの一致条件を指定し、地球が特定の文字で始まるか、または指定された配列に地球があるかどうかを確認します。The following example specifies the match conditions for the Exists method using lambda expressions to check whether a planet starts with a given letter or whether the planet is found on the given array.

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] planets = { "Mercury", "Venus",
                "Earth", "Mars", "Jupiter",
                "Saturn", "Uranus", "Neptune" };

            Console.WriteLine("One or more planets begin with 'M': {0}",
                Array.Exists(planets, element => element.StartsWith("M")));

            Console.WriteLine("One or more planets begin with 'T': {0}",
                Array.Exists(planets, element => element.StartsWith("T")));

            Console.WriteLine("Is Pluto one of the planets? {0}",
                Array.Exists(planets, element => element == "Pluto"));
        }
    }
}
// The example displays the following output:
//       One or more planets begin with 'M': True
//       One or more planets begin with 'T': False
//       Is Pluto one of the planets? False
Module Example
    Public Sub Main()
        Dim planets() As String = {"Mercury", "Venus",
                                    "Earth", "Mars", "Jupiter",
                                    "Saturn", "Uranus", "Neptune"}

        Console.WriteLine("One or more planets begin with 'M': {0}",
            Array.Exists(planets, Function(element)
                                      Return element.StartsWith("M")
                                  End Function))

        Console.WriteLine("One or more planets begin with 'T': {0}",
            Array.Exists(planets, Function(element)
                                      Return element.StartsWith("T")
                                  End Function))

        Console.WriteLine("Is Pluto one of the planets? {0}",
            Array.Exists(planets, Function(element)
                                      Return element.Equals("Pluto")
                                  End Function))

    End Sub
End Module
' The example displays the following output:
'       One or more planets begin with 'M': True
'       One or more planets begin with 'T': False
'       Is Pluto one of the planets? False

次の例では、Exists メソッドを使用して、文字列配列内のいずれかの名前が指定された文字で始まるかどうかを示します。The following example uses the Exists method to indicate whether any names in a string array begin with a specified character. この例では、検索する文字列をクラスコンストラクターに渡すことによって、@no__t 0 のオブジェクトをインスタンス化します。The example instantiates a StringSearcher object by passing the string to search for to its class constructor. @No__t-0 メソッドには、Predicate<T> デリゲートと同じシグネチャがあります。The StringSearcher.StartsWith method has same signature as the Predicate<T> delegate. @No__t-0 メソッドが呼び出されると、配列の各メンバーは、true を返すか、配列内のすべての要素を反復処理するまで、デリゲートに渡されます。When the Exists method is called, each member of the array is passed to the delegate until it returns true or iterates all the elements in the array.

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, (new StringSearcher(charToFind)).StartsWith));
   }
}

public class StringSearcher
{
   Char firstChar;

   public StringSearcher(Char firstChar)
   {
      this.firstChar = Char.ToUpper(firstChar);
   }

   public bool StartsWith(String s)
   {
      if (String.IsNullOrEmpty(s)) return false;

      if(s.Substring(0, 1).ToUpper() == firstChar.ToString())
         return true;
      else
         return false;
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
Module Example
   Public Sub Main()
      Dim names() As String = { "Adam", "Adel", "Bridgette", "Carla",
                                "Charles", "Daniel", "Elaine", "Frances",
                                "George", "Gillian", "Henry", "Irving",
                                "James", "Janae", "Lawrence", "Miguel",
                                "Nicole", "Oliver", "Paula", "Robert",
                                "Stephen", "Thomas", "Vanessa",
                                "Veronica", "Wilberforce" }
      Dim charsToFind() As Char = { "A"c, "K"c, "W"c, "Z"c }
      
      For Each charToFind In charsToFind
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, AddressOf (New StringSearcher(charToFind)).StartsWith))
      Next
   End Sub
   
End Module

Public Class StringSearcher
   Dim firstChar As Char
   
   Public Sub New(firstChar As Char)
      Me.firstChar = Char.ToUpper(firstChar)
   End Sub
   
   Public Function StartsWith(s As String) As Boolean
      If String.IsNullOrEmpty(s) Then Return False
      
      If s.Substring(0, 1).ToUpper = firstChar Then
         Return True
      Else
         Return False
      End If
   End Function
End Class
' The example displays the following output:
'       One or more names begin with 'A': True
'       One or more names begin with 'K': False
'       One or more names begin with 'W': True
'       One or more names begin with 'Z': False

また、ラムダ式を使用することもできます。これは、シグネチャがデリゲートのメソッドに対応するメソッドを明示的に定義するのではなく、You can also use a lambda expression rather than explicitly define a method whose signature corresponds to that of the delegate. 次の例では、@no__t 0 クラスとその StartsWith メソッドをラムダ式に置き換えます。The following example replaces the StringSearcher class and its StartsWith method with a lambda expression.

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names,
                                        s => { if (String.IsNullOrEmpty(s))
                                                  return false;

                                               if (s.Substring(0, 1).ToUpper() == charToFind.ToString())
                                                  return true;
                                               else
                                                  return false;
                                             } ));
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
Module Example
   Public Sub Main()
      Dim names() As String = { "Adam", "Adel", "Bridgette", "Carla",
                                "Charles", "Daniel", "Elaine", "Frances",
                                "George", "Gillian", "Henry", "Irving",
                                "James", "Janae", "Lawrence", "Miguel",
                                "Nicole", "Oliver", "Paula", "Robert",
                                "Stephen", "Thomas", "Vanessa",
                                "Veronica", "Wilberforce" }
      Dim charsToFind() As Char = { "A"c, "K"c, "W"c, "Z"c }

      For Each charToFind In charsToFind
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, Function(s)
                                                  If String.IsNullOrEmpty(s) Then Return False

                                                  If s.Substring(0, 1).ToUpper = charToFind Then
                                                     Return True
                                                  Else
                                                     Return False
                                                  End If
                                               End Function ))
      Next
   End Sub
End Module
' The example displays the following output:
'       One or more names begin with 'A': True
'       One or more names begin with 'K': False
'       One or more names begin with 'W': True
'       One or more names begin with 'Z': False

注釈

@No__t-0 は、渡されたオブジェクトがデリゲートで定義されている条件に一致する場合に true を返すメソッドのデリゲートです。The Predicate<T> is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. @No__t-0 の要素は、個別に Predicate<T> に渡され、一致が見つかったときに処理が停止されます。The elements of array are individually passed to the Predicate<T>, and processing is stopped when a match is found.

注意

およびC# Visual Basic では、Predicate<T> デリゲートを明示的に作成する必要はありません。In C# and Visual Basic, it is not necessary to create the Predicate<T> delegate explicitly. これらの言語は、コンテキストから正しいデリゲートを推測し、自動的に作成します。These languages infer the correct delegate from context and create it automatically.

このメソッドは O (n) 操作で、narray の です。This method is an O(n) operation, where n is the Length of array.

適用対象

こちらもご覧ください