Array.Exists<T> Method

Determines whether the specified array contains elements that match the conditions defined by the specified predicate.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool Exists<T>(
	T[] array,
	Predicate<T> match
)

Type Parameters

T

The type of the elements of the array.

Parameters

array
Type: T[]

The one-dimensional, zero-based Array to search.

match
Type: System.Predicate<T>

The Predicate<T> that defines the conditions of the elements to search for.

Return Value

Type: System.Boolean
true if array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.

ExceptionCondition
ArgumentNullException

array is null.

-or-

match is null.

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. The elements of array are individually passed to the Predicate<T>, and processing is stopped when a match is found.

NoteNote

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.

This method is an O(n) operation, where n is the Length of array.

The following example uses the Exists<T> method to indicate whether any names in a string array begin with a specified character. The example instantiates a StringSearcher object by passing the string to search for to its class constructor. The StringSearcher.StartsWith method has same signature as the Predicate<T> delegate. When the Exists<T> 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

You can also use a lambda expression rather than explicitly define a method whose signature corresponds to that of the delegate. 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
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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft