Home
Search
C# FirstOrDefault ExamplesUse the FirstOrDefault method. FirstOrDefault gets the first element if it exists.
FirstOrDefault. This C# method, part of System.Linq, is almost the same as First. The difference is how FirstOrDefault handles empty collections.
Method defaults. If a collection is empty, FirstOrDefault returns the default value for the type. The method internally checks if an element exists.
First
Last, LastOrDefault
Simple 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.
C# program that tests FirstOrDefault
using System; using System.Linq; class Program { static void Main() { 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
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.
String Literal
Part 4 The program displays 0 because that is the result of FirstOrDefault when called on an empty integer array.
C# program that uses FirstOrDefault
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // 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
Internals. The FirstOrDefault method is an extension method. It is a generic method which means it accepts a type parameter that indicates what types it acts upon.
Tip You do not need to specify the type parameter because this is inferred during the C# compilation step.
Null It checks the acting collection for null, and if that is true it returns the result of the default(T) expression.
And Otherwise, it returns the element at index 0 of the element iterated to first.
Default expression. The C# language provides a default expression, which the FirstOrDefault method refers to. This expression is similar to the typeof operator.
Info The default value for a value type is equal to all zero bits. And the default value for a reference type is the null value.
Default
A summary. FirstOrDefault is a way to access the "best match" from a query expression that sorts and filters elements. It protects against invalid accesses.
Home
© 2007-2022 sam allen.
see site info on the changelog.