C# Array IndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

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);
CallWhat 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

ParameterDescription
arrayRequired. The array to search.
valueRequired. The value to find.
startIndexOptional overload. Where to begin scanning.

Return Value

Return ValueDescription
indexFirst match position (0-based).
-1No 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);
    }
}

How It Works

  1. Left to right. Scanning starts at index 0.
  2. First hit wins. The second 30 at the end is ignored.
  3. No sort needed. Order does not matter for IndexOf.
  4. 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);
        }
    }
}

How It Works

  1. Sentinel. -1 means absence.
  2. Guard clause. Check before indexing pets[ix].
  3. Case sensitive. Default string equality cares about letter case.
  4. 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);
    }
}

How It Works

  1. First pass. IndexOf finds 5 at 0.
  2. Resume after. Starting at first + 1 skips the first hit.
  3. Second hit. Index 2 holds the next 5.
  4. 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).

Next: Initialize()

Re-run field initializers on value-type elements in the array with System.Array.

Initialize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful