How to Find the Position of an Item in a List with FindIndex in C#
shopping.FindIndex(item => item.StartsWith('m')) returns the position of the first item in the list that passes the test, here 1 for milk. When no item passes, it returns -1, so check for that before you use the position.
To change an item in a List<T>, you need its position. FindIndex finds it with a test that you write. Microsoft's reference: 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>. And what it returns: The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Syntax
int <index> = <list>.FindIndex(<item> => <condition>);
int <index> = <list>.FindIndex(<startIndex>, <item> => <condition>);
| Part | What it is |
|---|---|
<list> | A List<T>, such as a List<string> |
<item> => <condition> | The test: a lambda that gets each item and returns true for the one you want |
<startIndex> | Where to start looking. Without it, the search starts at the first item |
<index> | The position of the first item that passed, counted from 0, or -1 when none did |
The reference on the test: 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 Example
A console app called FindIndex has a shopping list of bread, milk, eggs and melon. It finds the first item that starts with m, replaces it, looks for the next one, and looks for an item that isn't there.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n FindIndex
cd FindIndex
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/FindIndex/FindIndex.csproj:
Determining projects to restore...
Restored /Users/you/source/FindIndex/FindIndex.csproj (in 47 ms).
Restore succeeded.
dotnet new console -n FindIndex creates a folder named FindIndex with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or FindIndex.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the list and the search:
// A shopping list. FindIndex returns the position of the first item that passes the test.
List<string> shopping = ["bread", "milk", "eggs", "melon"];
int index = shopping.FindIndex(item => item.StartsWith('m'));
Console.WriteLine($"First item that starts with m: {shopping[index]} at index {index}");
item => item.StartsWith('m') is the test. FindIndex passes each item to it, starting with the first, and returns the position of the first item for which it's true. Positions start at 0, so bread is at 0 and milk at 1.
Then use the position:
// The index lets you replace that item in the list.
shopping[index] = "oat milk";
Console.WriteLine($"Now: {string.Join(", ", shopping)}");
shopping[index] is the item at that position, so assigning to it replaces milk. string.Join puts the list on one line, with commas between the items.
Then the next match:
// Start the search after the first match to find the next one.
int next = shopping.FindIndex(index + 1, item => item.StartsWith('m'));
Console.WriteLine($"Next item that starts with m: {shopping[next]} at index {next}");
The first argument, index + 1, starts the search after milk, so it finds the next item that starts with m.
Last, a search with no match:
// When no item passes the test, FindIndex returns -1.
int missing = shopping.FindIndex(item => item.StartsWith('z'));
Console.WriteLine($"First item that starts with z: index {missing}");
if (missing < 0)
Console.WriteLine("Nothing on the list starts with z");
No item starts with z, so FindIndex returns -1, and missing < 0 is how you check for that.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
First item that starts with m: milk at index 1
Now: bread, oat milk, eggs, melon
Next item that starts with m: melon at index 3
First item that starts with z: index -1
Nothing on the list starts with z

Milk was at index 1, the second place, because positions start at 0. Assigning to shopping[1] changed it to oat milk. Starting at index 2, the next item that starts with m was melon, at index 3. No item starts with z, so that search returned -1.
The Complete File

// A shopping list. FindIndex returns the position of the first item that passes the test.
List<string> shopping = ["bread", "milk", "eggs", "melon"];
int index = shopping.FindIndex(item => item.StartsWith('m'));
Console.WriteLine($"First item that starts with m: {shopping[index]} at index {index}");
// The index lets you replace that item in the list.
shopping[index] = "oat milk";
Console.WriteLine($"Now: {string.Join(", ", shopping)}");
// Start the search after the first match to find the next one.
int next = shopping.FindIndex(index + 1, item => item.StartsWith('m'));
Console.WriteLine($"Next item that starts with m: {shopping[next]} at index {next}");
// When no item passes the test, FindIndex returns -1.
int missing = shopping.FindIndex(item => item.StartsWith('z'));
Console.WriteLine($"First item that starts with z: index {missing}");
if (missing < 0)
Console.WriteLine("Nothing on the list starts with z");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Using the position without checking for -1 | Assigning to shopping[missing], with missing at -1, threw ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') | Check index < 0 first, as the example does |
Testing index > 0 for "found" | Bread is at index 0, so the check printed Not found (index > 0) | Test index >= 0: only -1 means not found |
Calling FindIndex on an array | error CS1501: No overload for method 'FindIndex' takes 1 arguments | Use Array.FindIndex(shopping, item => item.StartsWith('m')), which returned 1 |
FAQ
How is FindIndex different from IndexOf?
IndexOf looks for an item that's equal to the one you pass, and FindIndex for the first item that passes your test. On the same list, IndexOf("eggs") returned 2, and IndexOf("Eggs") returned -1, because the capital E makes it a different string.
How do I find the last match?
FindLastIndex takes the same test and searches from the end. On the shopping list, the last item that starts with m was melon, at index 3.
Conclusion
FindIndex gives you the position of the first item in a List<T> that passes your test, and -1 when none does. Check for -1 before you use the position, and pass a start index to find the next match.
Additional Resources
List<T>.FindIndex— all three forms and what they return.List<T>.FindLastIndex— the same search, from the end.Array.FindIndex— the version for arrays.
