C# List IndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
List

Definition

In C#, List.IndexOf(item) tells you where a value is. It scans from the first item and returns the index of the first item that equals your value.

If nothing matches, it returns -1. That is a signal value, not an exception, so always check for it before you use the result as an index. For a custom test instead of equality, use FindIndex(). For the last match, use LastIndexOf().

This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.IndexOf(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.

Syntax

Pass the value. The two longer overloads add a start index, and optionally a count of items to look at.

C#
List<int> numbers = new List<int> { 5, 8, 5, 9 };
int first = numbers.IndexOf(5);        // 0
int next = numbers.IndexOf(5, 1);      // 2: start at index 1
int none = numbers.IndexOf(7);         // -1
CallWhat it does
numbers.IndexOf(5)Index of the first 5, or -1.
numbers.IndexOf(5, 1)Starts the scan at index 1 and goes to the end.
numbers.IndexOf(5, 1, 2)Looks only at 2 items, starting at index 1.

Equality is the default one (Equals), so strings are case-sensitive, and your own classes compare by reference unless they override Equals. A start index above Count, or a negative one, throws ArgumentOutOfRangeException. A start index equal to Count is allowed and returns -1.

Library declarations
C#
public int IndexOf(T item);
public int IndexOf(T item, int index);
public int IndexOf(T item, int index, int count);

Parameters

ParameterDescription
itemRequired. The value to look for.
indexOptional overload. The zero-based position where the search starts.
countOptional overload. How many items to search, beginning at index.

Return Value

Return ValueDescription
int (0 or more)The zero-based index of the first match.
-1No item matched.

C# Examples

Example 1 — Find the First Match

Search a list that holds a value twice. Only the first position is reported. A missing value gives -1.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 40, 10, 30, 20, 30 };

        Console.WriteLine(numbers.IndexOf(30));
        Console.WriteLine(numbers.IndexOf(10));
        Console.WriteLine(numbers.IndexOf(99));
    }
}

How It Works

  1. Left to right. The scan starts at index 0 and stops at the first 30, which sits at index 2.
  2. Second copy ignored. The other 30 at index 4 is never looked at, because the first hit ends the search.
  3. Any position. 10 is the second item, so its index is 1. The list does not need to be sorted.
  4. Missing means -1. There is no 99, so the result is -1 and no exception is thrown.

Example 2 — Find All Positions of a Value

Use the start-index overload in a loop to list every position of a value.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 5, 8, 5, 9, 5 };

        int pos = numbers.IndexOf(5);
        while (pos != -1) {
            Console.WriteLine("5 at index " + pos);
            pos = numbers.IndexOf(5, pos + 1);
        }
    }
}

How It Works

  1. First hit. IndexOf(5) returns 0, so the loop body prints the first position.
  2. Resume after the hit. IndexOf(5, pos + 1) starts one place later. That skips the match we already reported.
  3. Loop until -1. After index 4 the next search starts at 5, which equals Count. That is allowed, and it returns -1, so the loop ends.
  4. No extra list needed. The pattern works with one variable and never collects the positions into a second list.

Example 3 — Search Only Part of the List

Give a start index and a count to look at just a slice of the list. The same value outside that slice is ignored.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> items = new List<string> { "a", "b", "c", "b", "a" };

        Console.WriteLine(items.IndexOf("a", 1));
        Console.WriteLine(items.IndexOf("a", 1, 3));
        Console.WriteLine(items.IndexOf("b", 2, 2));
    }
}

How It Works

  1. From index 1 on. The first "a" at index 0 is skipped. The next one is at index 4.
  2. Count limits the end. (1, 3) looks at indexes 1, 2, and 3 only. The "a" at index 4 is outside, so the result is -1.
  3. Result is still absolute. IndexOf("b", 2, 2) checks indexes 2 and 3 and finds "b" at index 3. The number is a position in the whole list, not in the slice.
  4. Bad range errors. A start index above Count, or a count that runs past the end, throws an exception, so keep the numbers inside the list.

Frequently Asked Questions

The zero-based index of the first item that equals the value, or -1 when there is no match.
Contains returns only true or false. IndexOf also tells you where the item is, so you can read, replace, or remove it by index.
Use LastIndexOf, which scans from the end of the list towards the start.
Yes. It is a simple left-to-right scan, so the order does not matter. BinarySearch is the one that needs a sorted list.
IndexOf compares by equality with a value. FindIndex takes a predicate (a test lambda) so you can search with any rule.

Next: Insert()

Put one item at a chosen index and move the later items one place to the right.

Insert() 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.

7 people found this page helpful