Choose First and Single by the result cardinality

Choose a LINQ selection method from how many matching items the operation permits. SingleOrDefault allows no match, but it still rejects multiple matches. Its name does not mean “give me any value without throwing”.

Replacing it with FirstOrDefault can hide duplicate data. Make that change only when selecting the first match is the actual contract.

State the expected number of matches

Method Zero matches One match More than one match
First Throws Returns it Returns first
FirstOrDefault Returns default Returns it Returns first
Single Throws Returns it Throws
SingleOrDefault Returns default Returns it Throws

With a predicate, count matching elements rather than all source elements. These are Enumerable rules for in-memory sequences. A query provider can translate the operations differently, while preserving the intended result semantics.

Run zero, one and many cases

Use the .NET 10 SDK. Create a console project in a new directory, then replace Program.cs with the example below.

Plain text
dotnet new console --framework net10.0
dotnet run

Run dotnet run after saving the replacement. No additional packages are required.

C#
using System;
using System.Linq;

var cases = new[] { Array.Empty<int>(), new[] { 7 }, new[] { 7, 9 } };
foreach (var values in cases) {
	var first = Outcome(() => values.First());
	var firstOrDefault = Outcome(() => values.FirstOrDefault());
	var single = Outcome(() => values.Single());
	var singleOrDefault = Outcome(() => values.SingleOrDefault());
	Console.WriteLine($"Count={values.Length}: First={first}, FirstOrDefault={firstOrDefault}, Single={single}, SingleOrDefault={singleOrDefault}");
}
Console.WriteLine($"Explicit fallback: {Array.Empty<int>().SingleOrDefault(-1)}");
Console.WriteLine($"Unique match: {new[] { 7, 9, 9 }.Single(x => x == 7)}");

static string Outcome(Func<int> operation) {
	try {
		return operation().ToString(System.Globalization.CultureInfo.InvariantCulture);
	} catch (InvalidOperationException) {
		return "error";
	}
}

Expected output:

Plain text
Count=0: First=error, FirstOrDefault=0, Single=error, SingleOrDefault=0
Count=1: First=7, FirstOrDefault=7, Single=7, SingleOrDefault=7
Count=2: First=7, FirstOrDefault=7, Single=error, SingleOrDefault=error
Explicit fallback: -1
Unique match: 7

Outcome catches the expected exception only to keep all demonstration cases visible. Application code should normally let a violated uniqueness invariant surface at the appropriate boundary, with enough context to identify the duplicate. Do not adopt the helper as a general way to suppress selection failures.

Separate absence from a legitimate default

For int, default is zero. For a reference type, it is null. If zero is a valid identifier, a returned zero does not tell you whether an item existed. A nullable projection can preserve that distinction, or a suitable overload can supply an explicit fallback value.

The example uses the modern SingleOrDefault default-value overload , available from .NET 6. The fallback is used only for no match. It does not prevent an exception when two matches exist. A sentinel such as -1 is appropriate only when the valid domain excludes it.

Treat order and uniqueness as separate decisions

Use Single to express “exactly one configured primary account” and SingleOrDefault for “zero or one optional mapping”. If the operation asks for the newest eligible item, order it explicitly and use First or FirstOrDefault. Include a stable tie-breaker when equal timestamps can occur.

For a database query, use a unique constraint when uniqueness is a storage invariant. A LINQ check alone cannot prevent another transaction from inserting a duplicate between operations. Do not replace the invariant with an arbitrary first row just to make an exception disappear.

For an iterator, Single may have to inspect the remaining items to prove uniqueness, whereas First stops as soon as it has a match. Do not select the weaker contract only to shorten enumeration. Choose the correct contract, then address an expensive or unbounded source deliberately.

Source and further reading

This article revisits the problem discussed in the following original answer. The explanation and examples here are newly written for .NET 10.

Enumerable.Single Method (System.Linq)
Returns a single, specific element of a sequence.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.single?view=net-10.0
Enumerable.SingleOrDefault Method (System.Linq)
Returns a single, specific element of a sequence, or a default value if that element is not found.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.singleordefault?view=net-10.0
Enumerable.First Method (System.Linq)
Returns the first element of a sequence.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.first?view=net-10.0

Author: Herman Schoenfeld