In C#, List.FindLastIndex(match) returns the position of the last item that passes a test. The scan begins at the end of the list and moves towards index 0. It returns -1 when no item passes.
It is the backward twin of FindIndex(), and the "rule" version of LastIndexOf(). Use it to cut a list after the last item that matters.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.FindLastIndex(...). 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 a lambda. The overloads set where the backward search starts and how many items it may inspect.
C#
List<int> numbers = new List<int> { 4, 15, 22, 9, 3 };
int a = numbers.FindLastIndex(n => n > 10); // 2
int b = numbers.FindLastIndex(1, n => n > 10); // 1: start at index 1, go left
int c = numbers.FindLastIndex(n => n > 100); // -1
Call
What it does
numbers.FindLastIndex(n => n > 10)
Index of the right-most number above 10, or -1.
numbers.FindLastIndex(1, n => n > 10)
Starts at index 1 and searches backward to index 0.
numbers.FindLastIndex(3, 2, n => n < 10)
Starts at index 3 and inspects 2 items backward: indexes 3 and 2.
As with LastIndexOf, the startIndex is where the search begins, and it moves left. A start index that is outside the list throws ArgumentOutOfRangeException. The returned index is a position in the whole list.
Library declarations
C#
public int FindLastIndex(Predicate<T> match);
public int FindLastIndex(int startIndex, Predicate<T> match);
public int FindLastIndex(int startIndex, int count, Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that returns true for the item you want.
startIndex
Optional overload. The zero-based index where the backward search begins.
count
Optional overload. How many items to inspect, moving backward from startIndex.
Return Value
Return Value
Description
int (0 or more)
The zero-based index of the last item that passed the test.
-1
No item passed the test.
C# Examples
Example 1 — Find the Position of the Last Match
Find where the right-most big number is, and the -1 answer for a missing one.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 4, 15, 22, 9, 3 };
Console.WriteLine(numbers.FindIndex(n => n > 10));
Console.WriteLine(numbers.FindLastIndex(n => n > 10));
Console.WriteLine(numbers.FindLastIndex(n => n > 100));
}
}
Output
1
2
-1
How It Works
Forward scan.FindIndex meets 15 first, at index 1.
Backward scan.FindLastIndex starts at index 4 (3, fails), index 3 (9, fails), then index 2 (22, passes).
Two ends of one range. Both indexes point to items that pass. They are the first and last matches.
Missing returns -1. No number is above 100, so the final line is -1.
Example 2 — Search Backward from an Index
A start index ignores everything on its right. A count can shorten the search even more.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 4, 15, 22, 9, 3 };
Console.WriteLine(numbers.FindLastIndex(1, n => n > 10));
Console.WriteLine(numbers.FindLastIndex(0, n => n > 10));
Console.WriteLine(numbers.FindLastIndex(3, 2, n => n < 10));
Console.WriteLine(numbers.FindLastIndex(2, 2, n => n < 10));
}
}
Output
1
-1
3
-1
How It Works
From index 1. Item 15 at index 1 passes straight away, so the answer is 1. The big 22 on the right is out of reach.
From index 0. Only 4 is checked, and it fails, so the result is -1.
Start 3, look at 2. The checked indexes are 3 and 2. 9 at index 3 is below 10, so the answer is 3.
Start 2, look at 2. The checked indexes are 2 and 1. They hold 22 and 15, neither below 10, so the result is -1.
Example 3 — Trim Empty Items at the End
Find the last non-empty text, then remove every item after it with RemoveRange.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> cells = new List<string> { "a", "", "b", "", "" };
int last = cells.FindLastIndex(s => s != "");
cells.RemoveRange(last + 1, cells.Count - last - 1);
Console.WriteLine(string.Join("|", cells));
Console.WriteLine(cells.Count);
}
}
Output
a||b
3
How It Works
Last real value. Scanning from the end, two empty strings fail the test, and "b" at index 2 passes. So last is 2.
Everything after.last + 1 is 3, and cells.Count - last - 1 is 2, so RemoveRange(3, 2) deletes the two trailing blanks.
Middle blank stays. The empty string at index 1 is before the last real value, so it is kept. The output a||b shows it between the bars.
Empty list case. If every item were blank, last would be -1, and the same code would remove all items, which is what you want.
Frequently Asked Questions
The zero-based index of the last item that passes your test lambda, found by scanning from the end. It returns -1 if no item passes.
FindIndex scans from the start and returns the first match position. FindLastIndex scans from the end and returns the last match position.
Backward. FindLastIndex(3, test) checks index 3, then 2, 1, and 0.
LastIndexOf compares with one value by equality. FindLastIndex uses a predicate, so it can apply any rule.
Cutting off the tail of a list after the last useful item, such as removing trailing empty strings.