
You are using query expressions or collections in your C# program and want to obtain the first element in the evaluated query or collection in a single function call. The System.Linq namespace provides the FirstOrDefault extension method that will return the first value in the series, or the value zero or null if there is no first element.

Initially we note that the System.Linq namespace is required for easy usage of the FirstOrDefault extension method. The FirstOrDefault method will appear in Visual Studio's Intellisense feature by typing the period after a collection identifier that describes a type that implements IEnumerable. This means it will appear after a List variable, array variable of any type, and query expressions such as those implicitly typed with the var-keyword.
Var ExamplesThis C# program demonstrates the FirstOrDefault extension. It requires System.Linq.
Program that uses FirstOrDefault [C#]
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
//
// This List has three elements.
//
var list = new List<string>() { "Cat", "Rat", "Mouse" };
Console.WriteLine(list.FirstOrDefault());
//
// 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);
//
// 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());
//
// This array has no elements, so FirstOrDefault is zero.
//
int[] array = new int[0];
Console.WriteLine(array.FirstOrDefault());
}
}
Output
Cat
True
Mouse
0
Walkthrough. The FirstOrDefault method is invoked four times. The first time the method is invoked, it returns the value of the first string element in the List. The second time it is called, it returns null because the 'query1' expression returned no elements. The third usage returns the string literal 'Mouse' because that is the first element that matches the 'where' clause in the query. Finally, the program displays 0 because that is the result of FirstOrDefault when called on an empty integer array.

The FirstOrDefault method is not defined on the classes you normally use it on; instead, it is a syntax extension found in a separate location in the base class library. It is a generic method which means it accepts a type parameter that indicates what types it acts upon. You do not need to specify the type parameter because this is inferred during the C# compilation step. It checks the acting collection for null, and if that is true it returns the result of the default(T) expression. Otherwise, it returns the element at index 0 of the element iterated to first.
Default expression. The C# language provides a 'default' expression, which is the expression the FirstOrDefault method refers to. This expression is similar to the typeof operator, in that you can use default(int) or default(StringBuilder), which will return 0 and null respectively. 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
We explored the FirstOrDefault extension method in the C# programming language and the included System.Linq namespace. This method provides a shortcut to accessing the element that occurs first in the collection or query, while protecting against invalid accesses if there are no elements. It can be thought of as another way to access the "best match" from a query expression that sorts and filters elements.
LINQ Examples