In C#, Array.FindIndex returns the index of the first element that passes your predicate. If nothing matches, it returns -1.
Prefer FindIndex when you need to update that slot or slice around it. Prefer Find when you only need the element value.
This is a static member of System.Array. You call it with the class name, such as Array.FindIndex(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Pass the array and predicate. Check for -1 before indexing.
C#
int ix = Array.FindIndex(nums, x => x > 20);
Call
What it does
Array.FindIndex(nums, x => x > 20)
Index of first int > 20, or -1.
Array.FindIndex(nums, 1, x => x > 20)
Start scanning at index 1.
FindLastIndex searches from the end. IndexOf is for equality only.
Library declarations
C#
public static int FindIndex<T>(T[] array, Predicate<T> match);
Parameters
Parameter
Description
array
Required. Array to search.
match
Required. Predicate returning true for a match.
startIndex
Optional overload. Where to begin.
Return Value
Return Value
Description
index
First matching position.
-1
No match.
C# Examples
Example 1 — Index of First Match
Find where the first value greater than 20 sits.
C#
using System;
class Program {
static void Main() {
int[] nums = { 5, 12, 25, 8 };
int ix = Array.FindIndex(nums, x => x > 20);
Console.WriteLine("index = " + ix);
Console.WriteLine("value = " + nums[ix]);
}
}
Output
index = 2
value = 25
How It Works
Predicate scan. Indexes 0 and 1 fail the test.
Index 2 hits. 25 is greater than 20.
Use the index. Reading nums[ix] shows the element.
Safe here. We know a match exists; still check -1 in real code.
Example 2 — Return -1 When Absent
A predicate that never matches yields -1.
C#
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob" };
int ix = Array.FindIndex(names, n => n.StartsWith("Z"));
Console.WriteLine(ix);
}
}
Output
-1
How It Works
No Z names. Predicate always false.
Sentinel -1. Same pattern as IndexOf.
Do not index.names[-1] would throw.
Vs Find. Find would return null instead of -1.
Example 3 — Start Mid-Array
Skip earlier elements by passing a start index.
C#
using System;
class Program {
static void Main() {
int[] nums = { 30, 5, 40, 8 };
int ix = Array.FindIndex(nums, 1, x => x > 20);
Console.WriteLine(ix);
}
}
Output
2
How It Works
Start at 1. The leading 30 is ignored.
Next candidate. 5 fails; 40 succeeds at index 2.
Windowing. Useful after you already handled an earlier hit.
Count overloads. You can also limit how many elements to scan.
Frequently Asked Questions
It returns -1.
Find returns the element (or default). FindIndex returns the index (or -1).
IndexOf matches by equality. FindIndex matches by any predicate you supply.
Yes. Overloads accept startIndex and optional count.