What are LINQ Last(), and LastOrDefault() functions?
What are LINQ Last(), and LastOrDefault() functions?
Technical Content Writer | Blogger
Hi, this is Amrit Chandran. I'm a professional content writer. I have 3+ years of experience in content writing. I write content like Articles, Blogs, and Views (Opinion based content on political and controversial).
Last() 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#