Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Enumerable.First() / Last() / Single()

Enumerable.First() / Last() / Single()

LINQ extension methods for retrieving the first, last, or only element of a sequence. This page also covers the OrDefault variants, which return a default value when no element is found.

Syntax

using System.Linq;

// Returns the first element (throws if the sequence is empty).
T first = source.First();
T first = source.First(x => condition);

// Returns the first element (returns default if the sequence is empty).
T? firstOrDefault = source.FirstOrDefault();
T? firstOrDefault = source.FirstOrDefault(x => condition);

// Last() and Single() have the same overloads.
T last   = source.Last();
T single = source.Single();

Method List

MethodDescription
First()Returns the first element. Throws an exception if the sequence is empty.
FirstOrDefault()Returns the first element. Returns the default value (null or 0) if the sequence is empty.
Last()Returns the last element. Throws an exception if the sequence is empty.
LastOrDefault()Returns the last element. Returns the default value if the sequence is empty.
Single()Returns the only element. Throws an exception if there are zero or more than one matching elements.
SingleOrDefault()Returns the only element. Returns the default value if there are zero matches; throws if there are two or more.
ElementAt(index)Returns the element at the specified index. Throws an exception if the index is out of range.
ElementAtOrDefault(index)Returns the element at the specified index. Returns the default value if the index is out of range.

Sample Code

using System;
using System.Collections.Generic;
using System.Linq;

List<int> numbers = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6 };

// Get the first element.
Console.WriteLine(numbers.First());               // 3
Console.WriteLine(numbers.First(n => n > 4));    // 5

// Get the last element.
Console.WriteLine(numbers.Last());                // 6
Console.WriteLine(numbers.Last(n => n < 3));     // 2

// Get the only element matching the condition (throws if more than one match).
List<int> solo = new List<int> { 10, 20, 30 };
Console.WriteLine(solo.Single(n => n == 20));     // 20

// Returns a default value instead of throwing when the sequence is empty.
List<int> empty = new List<int>();
Console.WriteLine(empty.FirstOrDefault());        // 0 (default value for int)
Console.WriteLine(empty.FirstOrDefault(-1));      // -1 (custom default value, available in .NET 6+)

// The default value for a string is null.
List<string?> strs = new List<string?>();
Console.WriteLine(strs.FirstOrDefault() ?? "none"); // none

// Retrieve an element by index using ElementAt.
Console.WriteLine(numbers.ElementAt(2));           // 4
Console.WriteLine(numbers.ElementAtOrDefault(99)); // 0 (out of range)

Notes

First(), Last(), and Single() throw an InvalidOperationException when the sequence is empty or no element satisfies the condition. Use the OrDefault variants when you want to retrieve an element safely.

Single() also throws when more than one element matches the condition. It is useful for asserting a business rule that exactly one element must exist, but use First() when multiple matches are possible.

Starting with .NET 6, you can pass a default value directly, as in FirstOrDefault(defaultValue). To check the number of elements, see Enumerable.Count(). To check whether any elements exist, see Enumerable.Any().

If you find any errors or copyright issues, please .