Last

Just like with First, the .Last method will return the last element in an array or list.

Basic example

For this list, using .Last will return the last item in the array.

Live example: https://dotnetfiddle.net/kkCqi0

var numbers = new[] { 2, 4, 6, 8, 10 };

int last = numbers.Last();

//output: 10

Last with Predicate

The .Last method can also take a predicate so it can filter the array or list based on a particular condition and return the last element that meets that boolean condition.

Take the example below where we specify a number greater than 6. Since 6, 8, and 10 are all greater than 6. 10 will be returned as it's the last number in the list.

Live example: https://dotnetfiddle.net/LB4VHZ

var numbers = new[] { 2, 4, 6, 8, 10 };

int last = numbers.Last(i => i > 6);

//output: 10