Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ First(), Single() and Last() methods

Reference for LINQ First, FirstOrDefault, Single, SingleOrDefault, Last and ElementAt in C#: exceptions, defaults, predicates and when to use each.

The element operators pull a single item out of a sequence. First() returns the first element, Last() the last, Single() the only one, and ElementAt() the one at a position. Each of them has an OrDefault variant that returns a default value instead of throwing when nothing matches.

They differ in what they promise: First() says "at least one exists", while Single() says "exactly one exists" and throws if there are two or more. Choose the strictest operator that states your intent, so surprises in the data fail loudly.

C#
var first = users.First(u => u.IsAdmin);
var only = settings.SingleOrDefault(s => s.Key == "theme");
var third = items.ElementAtOrDefault(2);

Try it

C# Picking elements and the OrDefault forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# What each operator throws

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static T First<T>(this IEnumerable<T> source);
public static T First<T>(this IEnumerable<T> source, Func<T, bool> predicate);
public static T? FirstOrDefault<T>(this IEnumerable<T> source, T defaultValue);
public static T Single<T>(this IEnumerable<T> source, Func<T, bool> predicate);
public static T Last<T>(this IEnumerable<T> source);
public static T ElementAt<T>(this IEnumerable<T> source, int index);
public static T ElementAt<T>(this IEnumerable<T> source, Index index);

First, Last, Single and the OrDefault forms all have overloads with a predicate, and the OrDefault forms also have overloads with a custom defaultValue. ElementAtOrDefault() mirrors ElementAt().

Name Description
predicate Only elements for which it returns true are considered.
defaultValue Returned by OrDefault forms when nothing qualifies (instead of default(T)).
index Zero-based position, or an Index such as ^1 for the last element.

Returns: the selected element.

Errors: InvalidOperationException when the sequence is empty or nothing matches (First, Last, Single), and when more than one element matches (Single, SingleOrDefault). ArgumentOutOfRangeException from ElementAt() for an index outside the sequence. ArgumentNullException for a null source or predicate.

Usage notes

  • Pick by intent: First() = "there is at least one, give me the first"; Single() = "there must be exactly one"; Last() = the final element.
  • FirstOrDefault() cannot tell "missing" from "default". For int, 0 could be a real answer. Use a custom default, Any(), or an explicit check.
  • Nullable reference types: FirstOrDefault() returns T?, so the compiler asks you to handle null.
  • Performance: First() is O(1) for lists and arrays and stops at the first match otherwise. Last() is O(1) on IList<T> but walks the entire sequence otherwise. Single(predicate) always scans until it can rule out a second match, which is the whole sequence when there is exactly one.
  • Ordering: "first" means first in the source order. Combine with OrderBy() or use MinBy()/MaxBy() for the smallest or largest.
  • Database queries: SingleOrDefault() is a good way to look up by a unique key, but FirstOrDefault() often translates to a cheaper query.
  • Take(1) vs First(): Take(1) returns a sequence (possibly empty) and never throws.