In C#, List.LastIndexOf(item) finds the last match. It starts at the end of the list, moves towards the start, and returns the index of the first equal item it meets, which is the right-most one.
If the value is not in the list the result is -1. It is the mirror of IndexOf(), which finds the left-most match. It is handy when a value repeats and you care about the latest one.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.LastIndexOf(...). 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 longer overloads set where the backward search starts, and how many items it may look at.
C#
List<int> numbers = new List<int> { 5, 8, 5, 9, 5 };
int last = numbers.LastIndexOf(5); // 4
int before = numbers.LastIndexOf(5, 3); // 2: search backward from index 3
int none = numbers.LastIndexOf(7); // -1
Call
What it does
numbers.LastIndexOf(5)
Index of the right-most 5, or -1.
numbers.LastIndexOf(5, 3)
Starts at index 3 and searches backward to index 0.
numbers.LastIndexOf(5, 3, 2)
Starts at index 3 and looks at only 2 items backward: indexes 3 and 2.
The index in the overloads is where the search starts, and it moves left from there. This is the opposite of what many people expect, so read the declarations below. Equality is the default Equals check. The index must be inside the list or an ArgumentOutOfRangeException is thrown.
Library declarations
C#
public int LastIndexOf(T item);
public int LastIndexOf(T item, int index);
public int LastIndexOf(T item, int index, int count);
Parameters
Parameter
Description
item
Required. The value to look for.
index
Optional overload. The zero-based index where the backward search starts.
count
Optional overload. How many items to look at, moving backward from index.
Return Value
Return Value
Description
int (0 or more)
The zero-based index of the last match.
-1
No item matched.
C# Examples
Example 1 — Find the Last Match
Compare IndexOf and LastIndexOf on a list that holds a value three times.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 5, 8, 5, 9, 5 };
Console.WriteLine(numbers.IndexOf(5));
Console.WriteLine(numbers.LastIndexOf(5));
Console.WriteLine(numbers.LastIndexOf(7));
}
}
Output
0
4
-1
How It Works
Left-most for IndexOf. The first line shows index 0, where the scan from the left meets 5 first.
Right-most for LastIndexOf. The scan starts at the last slot, index 4, which already holds 5, so it returns 4.
Same list, different answer. Both methods look at the same items. Only the direction of the scan differs.
Missing value. No 7 exists, so LastIndexOf gives -1, just as IndexOf would.
Example 2 — Search Backward from a Position
Give a start index to ignore the items on its right. Add a count to limit how far back the search goes.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 5, 8, 5, 9, 5 };
Console.WriteLine(numbers.LastIndexOf(5, 3));
Console.WriteLine(numbers.LastIndexOf(5, 1));
Console.WriteLine(numbers.LastIndexOf(5, 3, 1));
}
}
Output
2
0
-1
How It Works
Start at index 3. The item there is 9, not a match. Next is index 2, which holds 5, so the result is 2. The 5 at index 4 is ignored.
Start at index 1. Index 1 holds 8. Moving left reaches index 0, which is 5, so the result is 0.
A count of 1.(5, 3, 1) looks only at index 3. That item is 9, so there is no match and the result is -1.
Direction matters. With IndexOf, a start index of 3 would search indexes 3 and 4. With LastIndexOf, it searches 3, 2, 1, and 0.
Example 3 — Remove the Last Copy of a Value
Use LastIndexOf to get the position, then remove that item by index. A guard avoids using -1.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> items = new List<string> { "a", "b", "a", "c", "a" };
int ix = items.LastIndexOf("a");
if (ix >= 0) {
items.RemoveAt(ix);
}
Console.WriteLine(string.Join(", ", items));
Console.WriteLine(items.LastIndexOf("z"));
}
}
Output
a, b, a, c
-1
How It Works
Get the position. The right-most "a" is at index 4, so ix is 4.
Guard first. The if (ix >= 0) check keeps the program from calling RemoveAt(-1) when the value is absent.
Only one copy goes. The two earlier "a" items stay, so the list prints a, b, a, c.
Missing again. A search for "z" prints -1. This shows why the guard is needed.
Frequently Asked Questions
It searches from the end of the list towards the start and returns the index of the last item that equals the value, or -1 if none matches.
IndexOf returns the first (left-most) match. LastIndexOf returns the last (right-most) match. When a value appears only once, both return the same index.
It is the starting point of a backward search. LastIndexOf(x, 3) looks at indexes 3, 2, 1, and 0.
It returns -1. Check for it before you use the result as an index.
Yes. Get the index with LastIndexOf, check that it is 0 or more, then call RemoveAt with it.