In C#, Array.IndexOf walks the array from the start (or from a starting index) and returns the first position where the value equals the one you asked for.
If nothing matches, it returns -1. The array does not need to be sorted. For a custom condition (not plain equality), use FindIndex with a predicate.
This is a static member of System.Array. You call it with the class name, such as Array.IndexOf(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Pass the array and the value. Check for -1 before using the index.
C#
int ix = Array.IndexOf(nums, 30);
int ix2 = Array.IndexOf(names, "Bob", 1);
Call
What it does
Array.IndexOf(nums, 30)
First index of 30, or -1.
Array.IndexOf(names, "Bob", 1)
Search starting at index 1.
LastIndexOf searches from the end. BinarySearch is faster only on sorted data.
Library declarations
C#
public static int IndexOf(Array array, object value);
public static int IndexOf<T>(T[] array, T value);
Parameters
Parameter
Description
array
Required. The array to search.
value
Required. The value to find.
startIndex
Optional overload. Where to begin scanning.
Return Value
Return Value
Description
index
First match position (0-based).
-1
No match.
C# Examples
Example 1 — Find the First Match
Locate 30 in an unsorted int array.
C#
using System;
class Program {
static void Main() {
int[] nums = { 40, 10, 30, 20, 30 };
int ix = Array.IndexOf(nums, 30);
Console.WriteLine("First 30 at index " + ix);
}
}
Output
First 30 at index 2
How It Works
Left to right. Scanning starts at index 0.
First hit wins. The second 30 at the end is ignored.
No sort needed. Order does not matter for IndexOf.
Store the index. Use it to read or update that slot.
Example 2 — Handle -1 When Absent
Branch when the value is not in the array.
C#
using System;
class Program {
static void Main() {
string[] pets = { "cat", "dog", "bird" };
int ix = Array.IndexOf(pets, "fish");
if (ix < 0) {
Console.WriteLine("Not found");
} else {
Console.WriteLine("Found at " + ix);
}
}
}
Output
Not found
How It Works
Sentinel.-1 means absence.
Guard clause. Check before indexing pets[ix].
Case sensitive. Default string equality cares about letter case.
Clear message. Print a friendly result for callers.
Example 3 — Skip the Beginning
Start searching after index 0 to find a later duplicate.
C#
using System;
class Program {
static void Main() {
int[] nums = { 5, 8, 5, 9 };
int first = Array.IndexOf(nums, 5);
int second = Array.IndexOf(nums, 5, first + 1);
Console.WriteLine("first = " + first);
Console.WriteLine("second = " + second);
}
}
Output
first = 0
second = 2
How It Works
First pass. IndexOf finds 5 at 0.
Resume after. Starting at first + 1 skips the first hit.
Second hit. Index 2 holds the next 5.
Pattern. Repeat to walk every occurrence.
Frequently Asked Questions
It returns -1. Always test for -1 before using the index.
No. IndexOf scans in order and works on unsorted data.
IndexOf is a linear scan and returns -1 when missing. BinarySearch needs sorted data and returns a negative complement when missing.
Use Array.LastIndexOf.
Yes. Overloads accept a startIndex (and optionally a count).