In C#, List.FindIndex(match) tells you where the first item that passes a test is. It scans from the start and returns that item's index, or -1 when no item passes.
It is the "rule" version of IndexOf(). IndexOf compares with one value, while FindIndex takes a lambda such as n => n > 10. Unlike Find(), its "not found" answer, -1, can never be confused with a real item.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.FindIndex(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda. Optional overloads add a start index, and a count of items to search.
C#
List<int> numbers = new List<int> { 4, 15, 22, 9, 30 };
int a = numbers.FindIndex(n => n > 10); // 1
int b = numbers.FindIndex(2, n => n > 10); // 2: start at index 2
int c = numbers.FindIndex(n => n > 100); // -1
Call
What it does
numbers.FindIndex(n => n > 10)
Index of the first number above 10, or -1.
numbers.FindIndex(2, n => n > 10)
Starts the scan at index 2.
numbers.FindIndex(2, 3, n => n > 10)
Looks at 3 items only, beginning at index 2.
A start index above Count (or a negative one) throws ArgumentOutOfRangeException. The index you get back is always a position in the whole list, even when you searched only a part. See FindLastIndex() to search from the end.
Library declarations
C#
public int FindIndex(Predicate<T> match);
public int FindIndex(int startIndex, Predicate<T> match);
public int FindIndex(int startIndex, int count, Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that returns true for the item you want.
startIndex
Optional overload. The zero-based index where the search begins.
count
Optional overload. The number of items to search, starting at startIndex.
Return Value
Return Value
Description
int (0 or more)
The zero-based index of the first item that passed the test.
-1
No item passed the test.
C# Examples
Example 1 — Find the Position of a Match
Ask where the first big number is, and handle the case where there is none.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 4, 15, 22, 9, 30 };
int ix = numbers.FindIndex(n => n > 10);
Console.WriteLine(ix);
int missing = numbers.FindIndex(n => n > 100);
Console.WriteLine(missing == -1 ? "no match" : "at " + missing);
}
}
Output
1
no match
How It Works
Rule, not value. The lambda n => n > 10 is tested on each item. 4 fails, 15 passes.
Position returned.15 is at index 1, so that is the answer. Items 22 and 30 are never needed.
-1 is the signal. Nothing is above 100, so the result is -1.
Safe handling. The program compares with -1 before using the number, and prints a friendly message instead of crashing.
Example 2 — Search from a Start Index
Skip the early items by starting the search later. Combine it with a count to limit the end too.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 4, 15, 22, 9, 30 };
Console.WriteLine(numbers.FindIndex(2, n => n > 10));
Console.WriteLine(numbers.FindIndex(3, n => n > 10));
Console.WriteLine(numbers.FindIndex(0, 3, n => n > 25));
}
}
Output
2
4
-1
How It Works
Begin at index 2. The first item looked at is 22, which passes, so the answer is 2. Index 1 (15) is skipped.
Begin at index 3.9 fails and 30 passes, so the answer is 4.
Count limits the range.(0, 3, ...) checks only indexes 0, 1, and 2. The 30 at index 4 is outside, so the result is -1.
Whole-list numbers. The returned index is relative to the start of the list, not to the start index, which makes it safe to use directly.
Example 3 — Find a Position, Then Replace the Item
Use the index to replace one item with the indexer. Only replace when the search succeeded.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Bella", "Chen" };
int ix = names.FindIndex(s => s.StartsWith("B"));
if (ix >= 0) {
names[ix] = "Bruno";
}
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(ix);
}
}
Output
Asha, Bruno, Bella, Chen
1
How It Works
Rule on text.s.StartsWith("B") matches both "Ben" and "Bella". The search stops at the first one, index 1.
Guard.ix >= 0 means a match was found. Without this check, a missing match would try to write to index -1.
Replace, not insert.names[ix] = "Bruno" overwrites the item at that index. The list keeps the same length.
Only the first."Bella" also starts with B, but it stays untouched, because FindIndex reports a single position.
Frequently Asked Questions
The zero-based index of the first item that passes your test lambda, or -1 if no item passes.
IndexOf looks for one specific value by equality. FindIndex takes a predicate, so it can search with any rule.
Find returns default(T) when nothing matches, which can look like a real value (0 or null). FindIndex returns -1, which is never a valid index.
No. It is always a position in the whole list, even when you give a start index or a count.
Use FindLastIndex, which scans backward from the end of the list.