In C#, Array.FindLast walks from the end of the array and returns the last element that makes your predicate true.
If nothing matches, you get default(T) (null for reference types, 0 for int). For the index instead of the value, use FindLastIndex.
This is a static member of System.Array. You call it with the class name, such as Array.FindLast(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Pass the array and predicate. Handle the default carefully.
C#
int hit = Array.FindLast(nums, x => x > 10);
Call
What it does
Array.FindLast(nums, x => x > 10)
Last int greater than 10, or 0 if none.
Find returns the first match; FindLast returns the last match.
Library declarations
C#
public static T FindLast<T>(T[] array, Predicate<T> match);
Parameters
Parameter
Description
array
Required. Array to search.
match
Required. Predicate for a match.
Return Value
Return Value
Description
T
Last matching element, or default(T).
C# Examples
Example 1 — Last Matching Number
Find the last int greater than 10.
C#
using System;
class Program {
static void Main() {
int[] nums = { 4, 25, 8, 30, 2 };
int hit = Array.FindLast(nums, x => x > 10);
Console.WriteLine(hit);
}
}
Output
30
How It Works
From the end. Scanning starts at 2, then 30.
30 matches. It is the last value > 10.
25 skipped as “last”. An earlier match is not returned.
Element, not index. The method returns 30 itself.
Example 2 — Default When Missing
Show null for a failed string search.
C#
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob" };
string hit = Array.FindLast(names, n => n.Length > 5);
Console.WriteLine(hit == null ? "(none)" : hit);
}
}
Output
(none)
How It Works
Both short. Ann and Bob have length 3.
Null default. Reference types return null.
Null-coalesce print. Avoid printing a blank null line.
Same idea as Find. Only the search direction differs.
Example 3 — Last Among Several Matches
Multiple even numbers; FindLast picks the rightmost.
C#
using System;
class Program {
static void Main() {
int[] nums = { 2, 3, 4, 5, 6 };
int hit = Array.FindLast(nums, x => x % 2 == 0);
Console.WriteLine(hit);
Console.WriteLine("Find would return " + Array.Find(nums, x => x % 2 == 0));
}
}
Output
6
Find would return 2
How It Works
Several evens. 2, 4, and 6 all match.
FindLast → 6. Rightmost even wins.
Find → 2. Leftmost even for contrast.
Choose intentionally. First vs last depends on your rule.
Frequently Asked Questions
default(T), such as null for strings or 0 for ints.
Find returns the first match; FindLast returns the last match.