Array.IndexOf in C#: Finding Elements Efficiently

The IndexOf method in C# provides a simple way to find the position of a specified element within an array. It belongs to the System.Array class and can be used with both one-dimensional and multidimensional arrays.

Array.IndexOf searches for the specified value and returns the index of its first occurrence. If the value isn’t found, the method returns -1.

Basic Syntax

For a one-dimensional array, the simplest form is:

int index = Array.IndexOf(array, value);

Here:

  • array is the array to search.
  • value is the element you’re looking for.
  • The return value is the zero-based index of the first matching element.
  • -1 is returned when no matching element exists.

For example:

using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 10, 20, 30, 40, 50 };

        int index = Array.IndexOf(numbers, 30);

        Console.WriteLine(index);
    }
}

Output:

2

The value 30 is located at index 2 because C# arrays use zero-based indexing.

Finding an Element That Doesn’t Exist

If the specified value isn’t present in the array, Array.IndexOf returns -1.

int[] numbers = { 10, 20, 30, 40, 50 };

int index = Array.IndexOf(numbers, 100);

Console.WriteLine(index);

Output:

-1

This makes it easy to determine whether an element exists:

if (Array.IndexOf(numbers, 30) != -1)
{
    Console.WriteLine("The value was found.");
}

Alternatively, you can store the result and compare it with -1:

int index = Array.IndexOf(numbers, 30);

if (index >= 0)
{
    Console.WriteLine($"Found at index {index}");
}
else
{
    Console.WriteLine("Value not found.");
}

Array.IndexOf Returns the First Match

An important characteristic of Array.IndexOf is that it returns the index of the first occurrence of the specified value.

int[] numbers = { 10, 20, 30, 20, 40 };

int index = Array.IndexOf(numbers, 20);

Console.WriteLine(index);

Output:

1

Although 20 appears at indexes 1 and 3, the method stops at the first matching occurrence and returns 1.

If you need to find a later occurrence, you can use an overload that specifies where the search should start.

Searching from a Specific Index

Array.IndexOf provides an overload that allows you to specify the starting index:

int index = Array.IndexOf(array, value, startIndex);

For example:

int[] numbers = { 10, 20, 30, 20, 40 };

int index = Array.IndexOf(numbers, 20, 2);

Console.WriteLine(index);

Output:

3

The search begins at index 2, so the 20 at index 1 is ignored.

This is particularly useful when you want to find multiple occurrences of a value.

For example:

int[] numbers = { 10, 20, 30, 20, 40, 20 };

int first = Array.IndexOf(numbers, 20);

int second = Array.IndexOf(numbers, 20, first + 1);

int third = Array.IndexOf(numbers, 20, second + 1);

Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(third);

Output:

1
3
5

Searching Within a Specific Range

Another overload lets you specify both a starting index and the number of elements to search:

int index = Array.IndexOf(array, value, startIndex, count);

Consider this example:

int[] numbers = { 10, 20, 30, 40, 50, 60 };

int index = Array.IndexOf(numbers, 50, 1, 3);

Console.WriteLine(index);

The search starts at index 1 and examines three elements:

Index:   1    2    3
Value:  20   30   40

Since 50 isn’t within that range, the result is:

-1

If we change the range:

int index = Array.IndexOf(numbers, 40, 1, 3);

Console.WriteLine(index);

The result is:

3

The startIndex and count parameters are useful when you don’t want to search the entire array.

Working with Strings

Array.IndexOf can also be used with arrays of strings.

string[] languages =
{
    "C#",
    "Java",
    "Python",
    "JavaScript"
};

int index = Array.IndexOf(languages, "Python");

Console.WriteLine(index);

Output:

2

The comparison uses the array’s element type and equality semantics. For strings, this means the search is case-sensitive in the normal String.Equals sense.

For example:

string[] languages = { "C#", "Java", "Python" };

int index = Array.IndexOf(languages, "python");

Console.WriteLine(index);

Output:

-1

"python" and "Python" aren’t considered equal for this search.

Searching for Objects

Array.IndexOf isn’t limited to primitive types. You can also search arrays containing objects.

For example:

Person person1 = new Person { Id = 1, Name = "Alice" };
Person person2 = new Person { Id = 2, Name = "Bob" };
Person person3 = new Person { Id = 3, Name = "Charlie" };

Person[] people = { person1, person2, person3 };

int index = Array.IndexOf(people, person2);

Console.WriteLine(index);

Output:

1

An important point here is that the method determines whether an element matches according to equality semantics. If you’re working with custom classes, you should understand how Equals and equality are implemented for your type.

For example, two separately created objects containing the same data aren’t automatically considered equal merely because their properties have identical values.

Array.IndexOf vs. Array.FindIndex

These methods are related, but they’re useful for different scenarios.

Array.IndexOf searches for a specific value:

int[] numbers = { 10, 20, 30, 40 };

int index = Array.IndexOf(numbers, 30);

Array.FindIndex allows you to search using a condition:

int[] numbers = { 10, 20, 30, 40 };

int index = Array.FindIndex(numbers, n => n > 25);

The result is:

2

because 30 is the first number greater than 25.

A useful rule is:

Use Array.IndexOf when you know the value you’re looking for. Use Array.FindIndex when you need to define a condition for what constitutes a match.

Important Considerations

1. Indexes are zero-based

The first element is at index 0:

string[] colors = { "Red", "Green", "Blue" };

Console.WriteLine(Array.IndexOf(colors, "Red"));   // 0
Console.WriteLine(Array.IndexOf(colors, "Green")); // 1
Console.WriteLine(Array.IndexOf(colors, "Blue"));  // 2

2. -1 means “not found”

Never assume that a returned index is always valid:

int index = Array.IndexOf(numbers, 100);

if (index != -1)
{
    Console.WriteLine(numbers[index]);
}

Checking the result before using it as an array index prevents an IndexOutOfRangeException.

3. It searches sequentially

Array.IndexOf performs a linear search through the specified portion of the array. In general, this means that searching an array can require examining many elements before finding a match.

Common Mistake: Confusing Index and Value

Consider:

int[] numbers = { 10, 20, 30 };

int index = Array.IndexOf(numbers, 20);

Here:

index = 1

The method returns the position of 20, not the value itself.

You can then use that position to retrieve the value:

Console.WriteLine(numbers[index]);

Output:

20

Conclusion

Array.IndexOf is a straightforward and useful method for locating values in C# arrays. Its most common form is:

int index = Array.IndexOf(array, value);

The method returns the zero-based index of the first matching element or -1 when the element isn’t found.

Its overloads also allow you to control where the search begins and how much of the array is examined. For simple value-based searches, Array.IndexOf is often the clearest solution. When the search requires a condition rather than an exact value, methods such as Array.FindIndex may be more appropriate.

For the complete list of overloads, parameter details, exceptions, and framework-specific behavior, see the official Microsoft Learn documentation for Array.IndexOf.