Array::Exists<T> Method (array<T>^, Predicate<T>^)
Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- array
-
Type:
array<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::Booleantrue if array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.
Type Parameters
- T
The type of the elements of the array.
| Exception | Condition |
|---|---|
| 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.
Note |
|---|
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 specifies the match conditions for the Exists<T>method usinglambda expressions to check whether a planet starts with a given letter or whether the planet is found on the given 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 theExists<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.
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 StringSearcherclass and its StartsWith method with a lambda expression.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
