Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Array.Exists(Of 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)

'Declaration
Public Shared Function Exists(Of T) ( _
	array As T(), _
	match As Predicate(Of T) _
) As Boolean

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(Of T)

The Predicate(Of 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 Nothing.

-or-

match is Nothing.

The Predicate(Of 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(Of T), and processing is stopped when a match is found.

NoteNote

In C# and Visual Basic, it is not necessary to create the Predicate(Of 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(Of T) method using lambda expressions to check whether a planet starts with a given letter or whether the planet is found on the given array.

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

The following example uses the Exists(Of 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(Of T) delegate. When the Exists(Of 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.

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. The following example replaces the StringSearcher class and its StartsWith method with a lambda expression.

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

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

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft