List<T>.FindIndex Method (Int32, Int32, Predicate<T>)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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<T> that starts at the specified index and contains the specified number of elements.

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

public int FindIndex(
	int startIndex,
	int count,
	Predicate<T> match


Type: System.Int32

The zero-based starting index of the search.

Type: System.Int32

The number of elements in the section to search.

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

match is null.


startIndex is outside the range of valid indexes for the List<T>.


count is less than 0.


startIndex and count do not specify a valid section in the List<T>.

The List<T> is searched forward starting at startIndex and ending at startIndex plus count minus 1, if count is greater than 0.

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:

public bool methodName(T obj)

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

public bool StartsWith(Employee e)

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 (that is, the members from index 0 to index Count - 1). 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".

using System;
using System.Collections.Generic;

public class Employee : IComparable
   public String Name { get; set; }
   public int Id { get; set; }

   public int CompareTo(Object o )
      Employee e = o as Employee;
      if (e == null)
         throw new ArgumentException("o is not an Employee object.");

      return Name.CompareTo(e.Name);

public class EmployeeSearch
   String _s;

   public EmployeeSearch(String s)
      _s = s;

   public bool StartsWith(Employee e)
      return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);

public class Example
   public static void Main()
      var employees = new List<Employee>();
      employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
                                           new Employee { Name = "Jill", Id = 3 },
                                           new Employee { Name = "Dave", Id = 5 },
                                           new Employee { Name = "Jack", Id = 8 },
                                           new Employee { Name = "Judith", Id = 12 },
                                           new Employee { Name = "Robert", Id = 14 },
                                           new Employee { Name = "Adam", Id = 1 } } );

      var es = new EmployeeSearch("J");
      Console.WriteLine("'J' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1, es.StartsWith));

      es = new EmployeeSearch("Ju");
      Console.WriteLine("'Ju' starts at index {0}",
                        employees.FindIndex(0, employees.Count - 1,es.StartsWith));
// The example displays the following output:
//       'J' starts at index 3
//       '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