List<(Of <(T>)>).FindIndex Method (Predicate<(Of <(T>)>))

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

Public Function FindIndex ( _
    match As Predicate(Of T) _
) As Integer
public int FindIndex(
    Predicate<T> match
)

Parameters

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.

Exceptions

Exception Condition
ArgumentNullException

match is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

The List<(Of <(T>)>) is searched forward starting at the first element 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.

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

Examples

The following example demonstrates the find methods for the List<(Of <(T>)>) class. The example for the List<(Of <(T>)>) class contains book objects, of class Book, using the data from the Sample XML File: Books (LINQ to XML). The FillList method in the example uses LINQ to XML to parse the values from the XML to property values of the book objects.

The following table describes the examples provided for the find methods.

Method

Example

Find(Predicate<(Of <(T>)>))

Finds a book by an ID using the IDToFind predicate delegate.

C# example uses an anonymous delegate.

FindAll(Predicate<(Of <(T>)>))

Find all books that whose Genre property is "Computer" using the FindComputer predicate delegate.

FindLast(Predicate<(Of <(T>)>))

Finds the last book in the collection that has a publish date before 2001, using the PubBefore2001 predicate delegate.

C# example uses an anonymous delegate.

FindIndex(Predicate<(Of <(T>)>))

Finds the index of first computer book using the FindComputer predicate delegate.

FindLastIndex(Predicate<(Of <(T>)>))

Finds the index of the last computer book using the FindComputer predicate delegate.

FindIndex(Int32, Int32, Predicate<(Of <(T>)>))

Finds the index of first computer book in the second half of the collection, using the FindComputer predicate delegate.

FindLastIndex(Int32, Int32, Predicate<(Of <(T>)>))

Finds the index of last computer book in the second half of the collection, using the FindComputer predicate delegate.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

See Also

Reference

List<(Of <(T>)>) Class

FindIndex Overload

System.Collections.Generic Namespace

Exists

Find

FindLast

FindAll

FindLastIndex

BinarySearch

IndexOf

LastIndexOf

Predicate<(Of <(T>)>)