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

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

static member Exists<'T> : 
        array:'T[] *
        match:Predicate<'T> -> bool

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.

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.

System_CAPS_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 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

Universal Windows Platform
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
Return to top
Show: