First and FirstOrDefault Methods
Learn how to choose one element from a collection using First and FirstOrDefault methods.
We'll cover the following
How to pick the first element of a collection
The First
and FirstOrDefault
methods return the first element in a collection. The First
method throws an exception if the collection is empty. Unlike First
, FirstOrDefault
returns a default value if the collection is empty.
Also, First
and FirstOrDefault
return the first element of a collection matching a condition.
Let’s find the oldest film we have watched. Then, let’s find the oldest one with a low rating.
Get hands-on with 1400+ tech skills courses.