What are LINQ Last(), and LastOrDefault() functions?
What are LINQ Last(), and LastOrDefault() functions?
361
12-Feb-2025
Updated on 12-Feb-2025
Ashutosh Kumar Verma
12-Feb-2025Last() vs LastOrDefault() in LINQ (C#)
Last() and LastOrDefault() both retrieve the last element from a collection, but they behave differently when no matching element is found.
Last()
Example-
LastOrDefault()
Example-
Finding Last Even Number, with Default Handling
When to use what?
Also, Read: Explain First(), FirstOrDefault() function in LINQ with C#