FirstOrDefault example. Here we show what happens when FirstOrDefault is used on a non-empty collection, and then an empty collection. If a collection is empty, a default value is returned.
using System;
using System.Linq;
int[] test = { 1, 2 };
Console.WriteLine("FIRST OR DEFAULT: {0}", test.FirstOrDefault());
int[] test2 = new int[0];
Console.WriteLine("FIRST OR DEFAULT: {0}", test2.FirstOrDefault());FIRST OR DEFAULT: 1
FIRST OR DEFAULT: 0
First example. Unlike FirstOrDefault, First() will throw an exception if there is no first value to be found. This makes First much more difficult to work with in most programs.
using System.Collections.Generic;
using System.Linq;
List<int> values = [10, 20, 30];
// This causes an exception as no value matches the predicate function.// There is no First value!
var result = values.First(a => a > 50);Unhandled exception. System.InvalidOperationException: Sequence contains no matching element
Complex example. The System.Linq namespace is required to use FirstOrDefault. The FirstOrDefault method can be used on any type that implements IEnumerable.
Part 1 The first time FirstOrDefault is invoked, it returns the value of the first string element in the List.
Part 2 The second time it is called, it returns null because the "query1" expression returned no elements.
Part 3 The third usage returns the string literal "Mouse" because that is the first element that matches the "where" clause in the query.
Part 4 The program displays 0 because that is the result of FirstOrDefault when called on an empty integer array.
using System;
using System.Collections.Generic;
using System.Linq;
// Part 1: this List has 3 elements.
var list = new List<string>() { "Cat", "Rat", "Mouse" };
Console.WriteLine(list.FirstOrDefault());
// Part 2: this query produces no results so FirstOrDefault is null.
var query1 = from element in list
where element.Length > 10
select element;
Console.WriteLine(query1.FirstOrDefault() == null);
// Part 3: this query produces one result, so FirstOrDefault is a string.
var query2 = from element in list
where element.Length > 3
select element;
Console.WriteLine(query2.FirstOrDefault());
// Part 4: this array has no elements, so FirstOrDefault is zero.
int[] array = new int[0];
Console.WriteLine(array.FirstOrDefault());Cat
True
Mouse
0
Generics. The First and FirstOrDefault methods are extension methods. There are generic methods that accept (or infer) a type parameter that indicates what types it acts upon.
Summary. First and FirstOrDefault are ways to access the "best match" from a query expression that sorts and filters elements. They protect against invalid accesses.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 21, 2024 (new example).