LastOrDefault
Just like with Last, the .LastOrDefault
method will return the last element in an array or list and if there are no items, it will return the default item based on the type of the array or list.
Basic example
For this list, using .LastOrDefault
will return the 0 as this is the default type for an integer. If the list were a list of objects, the default item will be null.
Live example: https://dotnetfiddle.net/lU7PU1
var numbers = new[] { 2, 4, 6, 8, 10 };
int value = numbers.LastOrDefault(n => n < 0)
//output: 0