0
0
CsharpHow-ToBeginner · 3 min read

How to Find Index of Element in List in C#

In C#, use the List<T>.IndexOf(element) method to find the index of an element in a list. It returns the zero-based index of the first occurrence or -1 if the element is not found.
📐

Syntax

The IndexOf method is called on a list object and takes one argument: the element you want to find. It returns an int representing the position of the element.

  • list.IndexOf(element): Finds the first index of element in list.
  • Returns -1 if the element is not found.
csharp
int index = list.IndexOf(element);
💻

Example

This example shows how to find the index of the number 3 in a list of integers. It prints the index if found or a message if not found.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
        int index = numbers.IndexOf(3);

        if (index != -1)
        {
            Console.WriteLine($"Element 3 found at index: {index}");
        }
        else
        {
            Console.WriteLine("Element not found in the list.");
        }
    }
}
Output
Element 3 found at index: 2
⚠️

Common Pitfalls

One common mistake is expecting IndexOf to find elements by value when the list contains complex objects without overriding Equals. Another is forgetting that IndexOf returns -1 if the element is missing, which can cause errors if not checked.

Also, IndexOf finds only the first occurrence. If you want all indexes, you need a loop.

csharp
using System;
using System.Collections.Generic;

class Person
{
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        if (obj is Person other)
        {
            return Name == other.Name;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return Name?.GetHashCode() ?? 0;
    }
}

class Program
{
    static void Main()
    {
        var people = new List<Person>
        {
            new Person { Name = "Alice" },
            new Person { Name = "Bob" }
        };

        var searchPerson = new Person { Name = "Alice" };

        // This will return -1 because searchPerson is a different object instance if Equals is not overridden
        int index = people.IndexOf(searchPerson);
        Console.WriteLine(index); // Output: 0 if Equals overridden, -1 otherwise

        // Correct way without overriding Equals: search by property
        index = people.FindIndex(p => p.Name == "Alice");
        Console.WriteLine(index); // Output: 0
    }
}
Output
0 0
📊

Quick Reference

Remember these tips when using IndexOf:

  • Returns the first index of the element or -1 if not found.
  • Works well with simple types like int, string.
  • For complex objects, consider overriding Equals or use FindIndex with a condition.
  • Index is zero-based (starts at 0).

Key Takeaways

Use List.IndexOf(element) to get the zero-based index of an element in a list.
IndexOf returns -1 if the element is not found, so always check the result before using it.
For complex objects, override Equals or use FindIndex with a predicate to find the correct index.
IndexOf returns the first occurrence only; use loops if you need all indexes.
Indexing in C# lists starts at zero, so the first element is at index 0.