Array.FindAll(Of T) Method (T(), Predicate(Of T))
Retrieves all the elements that match the conditions defined by the specified predicate.
Assembly: mscorlib (in mscorlib.dll)
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: T()An Array containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty Array.
Type Parameters
- T
The type of the elements of the array.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is null. -or- match is null. |
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 the elements that match the conditions are saved in the returned array.
This method is an O(n) operation, where n is the Length of array.
The following example creates an array of 50 random numbers with values that can range from 0 to 1,000. It then calls the FindAll(Of T) method with a lambda expression that returns the values that range from 300 to 600. Note that the lambda expression is passed a parameter named x; this represents the individual array member that is passed to the Predicate(Of T). Also note that the local lBound and uBound variables are accessible within the lambda expression.
Imports System.Collections.Generic Module Example Public Sub Main() ' Get an array of n random integers. Dim values() As Integer = GetArray(50, 0, 1000) Dim lBound As Integer = 300 Dim uBound As Integer = 600 Dim matchedItems() As Integer = Array.FindAll(values, Function(x) x >= lBound And x <= uBound) For ctr As Integer = 0 To matchedItems.Length - 1 Console.Write("{0} ", matchedItems(ctr)) If (ctr + 1) Mod 12 = 0 Then Console.WriteLine() Next End Sub Private Function GetArray(n As Integer, lower As Integer, upper As Integer) As Integer() Dim rnd As New Random() Dim list As New List(Of Integer) For ctr As Integer = 1 To n list.Add(rnd.Next(lower, upper + 1)) Next Return list.ToArray() End Function End Module ' The example displays output similar to the following: ' 542 398 356 351 348 301 562 599 575 400 569 306 ' 535 416 393 385
The following code example demonstrates the Find(Of T), FindLast(Of T), and FindAll(Of T) generic methods. An array of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The code example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus".
The Find(Of T) generic method traverses the array from the beginning, passing each element in turn to the EndsWithSaurus method. The search stops when the EndsWithSaurus method returns true for the element "Amargasaurus".
Note |
|---|
In C# and Visual Basic, it is not necessary to create the Predicate<string> delegate (Predicate(Of String) in Visual Basic) explicitly. These languages infer the correct delegate from context and create it automatically. |
The FindLast(Of T) generic method is used to search the array backward from the end. It finds the element "Dilophosaurus" at position 5. The FindAll(Of T) generic method is used to return an array containing all the elements that end in "saurus". The elements are displayed.
The code example also demonstrates the Exists(Of T) and TrueForAll(Of T) generic methods.
Imports System Public Class DinoDiscoverySet Public Shared Sub Main() Dim dinosaurs() As String = { "Compsognathus", _ "Amargasaurus", "Oviraptor", "Velociraptor", _ "Deinonychus", "Dilophosaurus", "Gallimimus", _ "Triceratops" } Dim GoMesozoic As New DinoDiscoverySet(dinosaurs) GoMesozoic.DiscoverAll() GoMesozoic.DiscoverByEnding("saurus") End Sub Private dinosaurs As String() Public Sub New(items() As String) dinosaurs = items End Sub Public Sub DiscoverAll() Console.WriteLine() For Each dinosaur As String In dinosaurs Console.WriteLine(dinosaur) Next dinosaur End Sub Public Sub DiscoverByEnding(Ending As String) Dim dinoType As Predicate(Of String) Select Case Ending.ToLower() Case "raptor" dinoType = AddressOf EndsWithRaptor Case "tops" dinoType = AddressOf EndsWithTops Case "saurus" dinoType = AddressOf EndsWithSaurus Case Else dinoType = AddressOf EndsWithSaurus End Select Console.WriteLine(vbNewLine + _ "Array.Exists(dinosaurs, ""{0}""): {1}", _ Ending, _ Array.Exists(dinosaurs, dinoType)) Console.WriteLine(vbNewLine + _ "Array.TrueForAll(dinosaurs, ""{0}""): {1}", _ Ending, _ Array.TrueForAll(dinosaurs, dinoType)) Console.WriteLine(vbNewLine + _ "Array.Find(dinosaurs, ""{0}""): {1}", _ Ending, _ Array.Find(dinosaurs, dinoType)) Console.WriteLine(vbNewLine + _ "Array.FindLast(dinosaurs, ""{0}""): {1}", _ Ending, _ Array.FindLast(dinosaurs, dinoType)) Console.WriteLine(vbNewLine + _ "Array.FindAll(dinosaurs, ""{0}""):", Ending) Dim subArray() As String = _ Array.FindAll(dinosaurs, dinoType) For Each dinosaur As String In subArray Console.WriteLine(dinosaur) Next dinosaur End Sub ' Search predicate returns true if a string ends in "saurus". Private Function EndsWithSaurus(s As String) As Boolean ' AndAlso prevents evaluation of the second Boolean ' expression if the string is so short that an error ' would occur. If (s.Length > 5) AndAlso _ (s.ToLower().EndsWith("saurus")) Then Return True Else Return False End If End Function ' Search predicate returns true if a string ends in "raptor". Private Function EndsWithRaptor(s As String) As Boolean ' AndAlso prevents evaluation of the second Boolean ' expression if the string is so short that an error ' would occur. If (s.Length > 5) AndAlso _ (s.ToLower().EndsWith("raptor")) Then Return True Else Return False End If End Function ' Search predicate returns true if a string ends in "tops". Private Function EndsWithTops(s As String) As Boolean ' AndAlso prevents evaluation of the second Boolean ' expression if the string is so short that an error ' would occur. If (s.Length > 3) AndAlso _ (s.ToLower().EndsWith("tops")) Then Return True Else Return False End If End Function End Class ' This code example produces the following output: ' ' Compsognathus ' Amargasaurus ' Oviraptor ' Velociraptor ' Deinonychus ' Dilophosaurus ' Gallimimus ' Triceratops ' ' Array.Exists(dinosaurs, "saurus"): True ' ' Array.TrueForAll(dinosaurs, "saurus"): False ' ' Array.Find(dinosaurs, "saurus"): Amargasaurus ' ' Array.FindLast(dinosaurs, "saurus"): Dilophosaurus ' ' Array.FindAll(dinosaurs, "saurus"): ' Amargasaurus ' Dilophosaurus
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
