FindIndex Method (Predicate(T))
Collapse the table of content
Expand the table of content

List<T>::FindIndex Method (Predicate<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 entire List<T>.

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

public:
int FindIndex(
	Predicate<T>^ match
)

Parameters

match
Type: System::Predicate<T>^

The Predicate<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.

The List<T> is searched forward starting at the first element and ending at the last element.

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

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

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

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

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

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<T>^) method twice to search the entire collection , the first time for the first Employee object whose Name field begins with "J", and the second time for the first Employee object whose Name field begins with "Ju".

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

Universal Windows Platform
Available since 4.5
.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:
© 2016 Microsoft