List(Of T).FindIndex Method (Int32, Predicate(Of T))

 

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List(Of T) that extends from the specified index to the last element.

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

Public Function FindIndex (
	startIndex As Integer,
	match As Predicate(Of T)
) As Integer

Parameters

startIndex
Type: System.Int32

The zero-based starting index of the search.

match
Type: System.Predicate(Of T)

The Predicate(Of T) delegate that defines the conditions of the element to search for.

Return Value

Type: System.Int32

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.

Exception Condition
ArgumentNullException

match is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for the List(Of T).

The List(Of T) is searched forward starting at startIndex and ending at the last element.

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 the current List(Of T) are individually passed to the Predicate(Of T) delegate. The delegate has the signature:

Public Function methodName(obj As T) As Boolean

This method performs a linear search; therefore, this method is an O(n) operation, where n is the number of elements from startIndex to the end of the List(Of T).

The following example defines an Employee class with two fields, Name and Id. It also defines an EmployeeSearch class with a single method, StartsWith, that indicates whether the Employee.Name field starts with a specified substring that is supplied to the EmployeeSearch class constructor. Note the signature of this method

Public Function StartsWith(e As Employee) As Boolean

corresponds to the signature of the delegate that can be passed to the FindIndex method. The example instantiates a List<Employee> object, adds a number of Employee objets to it, and then calls the FindIndex(Int32, Int32, Predicate(Of T)) method twice to search the collection starting with its fifth member (that is, the member at index 4). The first time, it searches for the first Employee object whose Name field begins with "J"; the second time, it searches for the first Employee object whose Name field begins with "Ju".

Imports System.Collections.Generic

Public Class Employee : Implements IComparable
   Public Property Name As String
   Public Property Id As Integer

   Public Function CompareTo(o As Object) As Integer _
         Implements IComparable.CompareTo
      Dim e As Employee = TryCast(o, Employee)
      If e Is Nothing Then
         Throw New ArgumentException("o is not an Employee object.")
      End If

      Return Name.CompareTo(e.Name)
   End Function
End Class

Public Class EmployeeSearch
   Dim _s As String

   Public Sub New(s As String)
      _s = s
   End Sub

   Public Function StartsWith(e As Employee) As Boolean
      Return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase)
   End Function
End Class

Module Example
   Public Sub Main()
      Dim employees As New List(Of Employee)()
      employees.AddRange( { New Employee() With { .Name = "Frank", .Id = 2 },
                            New Employee() With { .Name = "Jill", .Id = 3 },
                            New Employee() With { .Name = "Dave", .Id = 5 },
                            New Employee() With { .Name = "Jack", .Id = 8 },
                            New Employee() With { .Name = "Judith", .Id = 12 },
                            New Employee() With { .Name = "Robert", .Id = 14 },
                            New Employee() With { .Name = "Adam", .Id = 1 } } )
      employees.Sort()

      Dim es As New EmployeeSearch("J")
      Dim index As Integer = employees.FindIndex(4, AddressOf es.StartsWith)        
      Console.WriteLine("Starting index of'J': {0}",
                        If(index >= 0, index.ToString(), "Not found"))

      es = New EmployeeSearch("Ju")
      index = employees.FindIndex(4, AddressOf es.StartsWith) 
      Console.WriteLine("Starting index of'Ju': {0}",
                        If(index >= 0, index.ToString(), "Not found"))

   End Sub
End Module
' The example displays the following output:
'       'J' starts at index 4
'       'Ju' starts at index 5

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: