In C#, List.FindLast(match) returns the last item that passes a test. It starts at the end of the list and moves left, and it stops at the first item it meets that makes the test true.
That is the same as asking for the right-most match. It is the mirror image of Find(). Like Find, it returns default(T) (0 or null) and does not throw when nothing passes.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.FindLast(...). 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 item itself is returned, not its index.
C#
List<int> numbers = new List<int> { 4, 15, 22, 9, 3 };
int lastBig = numbers.FindLast(n => n > 10); // 22
int none = numbers.FindLast(n => n > 100); // 0 (default)
Call
What it does
numbers.FindLast(n => n > 10)
The right-most number above 10.
names.FindLast(s => s.Length > 3)
The last long name, or null.
logs.FindLast(l => l.Level == "error")
The most recent error entry, if items are added in time order.
FindLast only moves in one direction: from the end towards the start. Use FindLastIndex() when you need the position. A null predicate throws ArgumentNullException. Array.FindLast is the static version for arrays.
Library declarations
C#
public T FindLast(Predicate<T> match);
Parameters
Parameter
Description
match
Required. A Predicate<T>: a lambda or method that takes one item and returns true for the item you want. It must not be null.
Return Value
Return Value
Description
T
The last item that passes the test.
default(T)
No item passed. 0 for numbers, null for classes and strings.
C# Examples
Example 1 — Find the Last Big Number
Compare Find and FindLast on the same list and the same test.
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.Find(n => n > 10));
Console.WriteLine(numbers.FindLast(n => n > 10));
Console.WriteLine(numbers.FindLast(n => n < 0));
}
}
Output
15
22
0
How It Works
Find goes left to right. It meets 15 first, so it returns 15.
FindLast goes right to left. It tests 3 (false), then 9 (false), then 22 (true) and stops.
Same test, different winner. Both numbers pass the rule. Only the search direction decides which one is returned.
No negatives. Nothing is below 0, so the last line prints the default 0.
Example 2 — Find the Last Long Name
Find the last name with more than three letters. Handle a missing name with a null check.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Ann", "Carla", "Bob", "Diana", "Ed" };
string last = names.FindLast(s => s.Length > 3);
string none = names.FindLast(s => s.Length > 9);
Console.WriteLine(last);
Console.WriteLine(none ?? "(none)");
}
}
Output
Diana
(none)
How It Works
Scan from "Ed". The scan starts at the end. "Ed" has 2 letters and fails, then "Diana" with 5 letters passes.
Earlier match ignored."Carla" also has more than three letters, but the scan has already stopped at "Diana".
No match gives null. No name has more than nine letters, so none is null (the default for string).
Null-coalescing.none ?? "(none)" prints the text on the right when the left side is null. This avoids a crash and prints a clear result.
Example 3 — Find the Latest Error
A list of log entries is added in time order, so FindLast gives the most recent entry of a given level.
C#
using System;
using System.Collections.Generic;
class Log {
public string Level;
public string Text;
}
class Program {
static void Main() {
List<Log> logs = new List<Log> {
new Log { Level = "info", Text = "start" },
new Log { Level = "error", Text = "disk full" },
new Log { Level = "info", Text = "retry" },
new Log { Level = "error", Text = "timeout" },
new Log { Level = "info", Text = "done" }
};
Log latest = logs.FindLast(l => l.Level == "error");
Console.WriteLine(latest.Text);
Console.WriteLine(logs.FindLast(l => l.Level == "fatal") == null);
}
}
Output
timeout
True
How It Works
Time order. Items were added oldest to newest, so the end of the list holds the newest entry.
Skips the newest info. The scan starts at "done" (info, fails) and moves left until it reaches the "timeout" error.
Older error unused. The "disk full" error also matches, but it is further left and never reached.
Nothing found. No entry has the level "fatal", so FindLast returns null, and the comparison prints True.
Frequently Asked Questions
The last item that passes your test lambda. If none passes, it returns default(T): 0 for numbers and null for classes.
Find scans from the start and returns the first match. FindLast scans from the end and returns the last match. If only one item matches, both return it.
No. It returns default(T). Check for null, or use FindLastIndex, which returns -1.
Use FindLastIndex with the same predicate.
Yes. When items are added in time order, FindLast returns the most recent item that fits a rule.