Search⌘ K
AI Features

First and FirstOrDefault Methods

Explore how to select the first element in a collection using LINQ's First and FirstOrDefault methods. Understand how these methods behave with empty collections and conditions, and learn their use alongside related methods like Last and Single to efficiently work with data in C#.

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 ...