FirstOrDefault is a LINQ functionality to return first element of the collection or default value if requested item does not exist. In case of collection of reference type objects the default is null, whilst in case of value types the default depends on the particular type (e.g. for int it is 0).
FirstOrDefault is overloaded method which can be used with either zero or one parameter. The first option just returns first element and the second one allows to define condition which needs to be met.
1. Collection of integers – no condition
Let’s create example collection of integers:
1 |
var numbers = new List<int> { 3, 5, 8, 13, 21 }; |
And execute FirstOrDefault function to get first element:
1 |
var firstNumber = numbers.FirstOrDefault(); |
Print to screen:
1 |
Console.WriteLine("First is: " + firstNumber); |
Result:
1 |
First is: 3 |
Now let’s clear the list to see what happens if we run FirstOrDefault on empty collection.
1 |
numbers.Clear(); |
Execute FirstOrDefault:
1 |
var firstNumberOrDefault = numbers.FirstOrDefault(); |
Print to screen:
1 |
Console.WriteLine("First is: " + firstNumberOrDefault); |
Result:
1 |
First is: 0 |
The result is 0 because there was no element on the list, so query returned default value of int type.
2. Collection of integers – with condition
Let’s use the same collection as in above example:
1 |
var numbers = new List<int> { 3, 5, 8, 13, 21 }; |
And use FirstOrDefault with a condition to get first item greater than 5:
1 |
var firstGreaterThanFive = numbers.FirstOrDefault(x => x > 5); |
Print to screen:
1 |
Console.WriteLine("First is: " + firstGreaterThanFive); |
Result:
1 |
First is: 8 |
Now let’s change a condition and try to get first element greater than 50:
1 |
var firstGreaterThanFifty = numbers.FirstOrDefault(x => x > 50); |
Print to screen:
1 |
Console.WriteLine("First is: " + firstGreaterThanFifty); |
Result:
1 |
First is: 0 |
The result is 0 because numbers collection doesn’t contain any item greater than 50.
3. Collection of objects – no condition
Let’s create example Car class.
1 2 3 4 5 |
public class Car { public string Color { get; set; } public int Seats { get; set; } } |
And collection of cars.
1 2 3 4 5 6 |
var cars = new List<Car> { new Car() { Color = "Red", Seats = 2 }, new Car() { Color = "Blue", Seats = 5 }, new Car() { Color = "Green", Seats = 4 } }; |
Now use FirstOrDefault to get first car:
1 |
var first = cars.FirstOrDefault(); |
Print to screen:
1 |
Console.WriteLine(first.Color + ", " + first.Seats); |
Result:
1 |
Red, 2 |
Let’s clear the list and see what happens if we run FirstOrDefault on empty cars collection.
1 |
cars.Clear(); |
Get first element or default value:
1 |
var firstOrDefaultCar = cars.FirstOrDefault(); |
Print to screen:
1 |
Console.WriteLine(firstOrDefaultCar.Color + ", " + firstOrDefaultCar.Seats); |
When we try to run it our application crashes because of NullReferenceException. The reason is we try to access Color and Seats properties not having the instance of Car object. As the collection is empty the variable firstOrDefaultCar equals null.
When using FirstOrDefault it is recommended to validate whether the result is actual value or default one.
1 2 |
if (firstOrDefaultCar == null) Console.WriteLine("There are no cars on the list."); |
Now the application works without any crashes and the result is:
1 |
There are no cars on the list. |
4. Collection of objects – with condition
Let’s create yet another collection of cars. It’s very similar to the one from previous example but now we’ve got two blue cars.
1 2 3 4 5 6 |
var cars = new List<Car> { new Car() { Color = "Red", Seats = 2 }, new Car() { Color = "Blue", Seats = 5 }, new Car() { Color = "Blue", Seats = 4 } }; |
We want to get first blue car, so need to call FirstOrDefault with appropriate condition:
1 |
var blueCar = cars.FirstOrDefault(x => x.Color == "Blue"); |
Print to screen:
1 |
Console.WriteLine(blueCar.Color + ", " + blueCar.Seats); |
Result:
1 |
Blue, 5 |
Now let’s change a condition and try to get first yellow car from the list:
1 |
var yellowCar = cars.FirstOrDefault(x => x.Color == "Yellow"); |
Print to screen:
1 |
Console.WriteLine(yellowCar.Color + ", " + yellowCar.Seats); |
Application crashes again because of NullReferenceException. It is because there is no yellow car on the list.
We can validate it with appropriate if statement:
1 2 |
if (yellowCar == null) Console.WriteLine("There is no yellow car on the list."); |
Result:
1 |
There is no yellow car on the list. |