C# Single vs First: Key Differences and Usage
Understand the differences between C# LINQ Single and First: exception behavior, scanning tradeoffs, and when to use each.

When you need to pull one element out of a sequence in C#, LINQ offers Single and First. The choice between them is not just about syntax; it changes the runtime behavior and the errors your code can throw. Both methods return an element from a sequence, but they enforce different contracts about the sequence's contents. Understanding those contracts is essential for writing correct and maintainable code.
What Single and First Actually Do
First returns the first element of a sequence. It only needs to examine the first element, so it works on any sequence that has at least one item. If the sequence is empty, it throws InvalidOperationException.
var numbers = new[] { 10, 20, 30 };
int first = numbers.First(); // returns 10Single returns the only element of a sequence. It requires that the sequence contains exactly one element. If the sequence is empty or contains more than one element, it throws InvalidOperationException.
var oneNumber = new[] { 42 };
int single = oneNumber.Single(); // returns 42Both methods also have overloads that accept a predicate. First(predicate) returns the first element that matches the condition. Single(predicate) returns the only element that matches the condition, and it throws if more than one match exists.
var numbers = new[] { 10, 20, 30 };
int firstOverFifteen = numbers.First(n => n > 15); // returns 20
int singleOverFifteen = numbers.Single(n => n > 15); // throws because 20 and 30 both matchThe key distinction is that First assumes you only care about the first match, while Single asserts that there is exactly one match. This assertion is a runtime check that can catch bugs early, but it also imposes a cost.
Exception Behavior: The Core Difference
The most important difference between Single and First is how they handle unexpected sequence contents. First throws only when the sequence is empty. Single throws both when the sequence is empty and when it contains more than one element. This makes Single a stricter contract.
Consider a scenario where you expect a database query to return exactly one record, such as a user by ID. If the data is corrupted and two records have the same ID, Single will throw, alerting you to the problem. First would silently return the first one, potentially hiding the issue.
try
{
var user = db.Users.Single(u => u.Id == requestedId);
}
catch (InvalidOperationException ex)
{
// Handle the fact that zero or multiple users were found
}Using First in the same situation would not throw when multiple users exist. It would return the first one, and you would have no indication that your data violates the expected uniqueness constraint. This is why Single is often the safer choice when you believe the sequence should contain exactly one element.
Performance: What Each Method Has to Scan
The practical performance difference is not that First is always faster; it is about how much of the source each method has to inspect before it can return or throw.
First() can return after reading the first element. Single() must also confirm that there is no second element, so the parameterless overload checks at most two elements. First(predicate) returns as soon as it finds the first match; if there is no match, it has to scan the whole sequence. Single(predicate) must inspect every element when there is exactly one match, because it has to prove that no second match exists. If the sequence contains more than one match, it can stop with an exception.
For in-memory collections, the extra work is usually negligible. For lazily evaluated sources, such as an IEnumerable<T> that performs an expensive computation per element, the difference can matter.
// First(predicate): stops at the first element where IsActive is true.
var firstMatch = largeSequence.First(x => x.IsActive);
// Single(predicate): inspects the source to confirm there is only one match.
var onlyMatch = largeSequence.Single(x => x.IsActive);How much work Single actually does depends on the source. For example, a database query provider may translate Single into its own SQL rather than fetching an entire table, so the overhead is not necessarily "download all rows." The general point is that Single(predicate) carries more validation cost than First(predicate), especially for lazy in-memory sequences.
Choosing Between Single and First
The decision comes down to what you know about the sequence and what you want to happen if your assumption is wrong. Use First when:
- You only need the first matching element.
- Multiple matches are acceptable, and an empty sequence is either handled separately or treated as an error.
- You want to avoid inspecting more of the sequence than necessary.
Use Single when:
- You expect exactly one element, and its absence or duplication indicates a bug.
- You want the code to fail loudly if the sequence does not meet the expectation.
- You are working with a domain where uniqueness is a business rule, such as a primary key lookup.
A common pattern is to use Single for operations that should return a unique record and First for operations that return the first record in a sorted or filtered set. For example, getting a user by ID should use Single because IDs are unique. Getting the most recent order from a list of orders sorted by date could use First because there is no expectation of uniqueness.
SingleOrDefault and FirstOrDefault: Handling Empty Sequences
Both Single and First have OrDefault variants that return default(T) instead of throwing when the sequence is empty. FirstOrDefault returns the first element or default if the sequence is empty. SingleOrDefault returns the single element, default if the sequence is empty, but still throws if there is more than one element.
var empty = new int[0];
int firstOrDefault = empty.FirstOrDefault(); // 0
int singleOrDefault = empty.SingleOrDefault(); // 0
var twoItems = new[] { 1, 2 };
int singleOrDefaultThrows = twoItems.SingleOrDefault(); // throws InvalidOperationExceptionThese variants are useful when you want to handle the empty case without catching an exception, but you still want to enforce uniqueness when elements exist. SingleOrDefault is a good choice for a lookup that may return no record but should never return multiple records.
Common Mistakes When Using Single
A frequent mistake is using Single on a sequence that is not guaranteed to have exactly one element. For example, if you filter a collection with a predicate that can match multiple items, Single will throw. Developers sometimes use Single thinking it means "the first one" and are surprised by the exception.
Another mistake is using Single on a lazy sequence that is expensive to enumerate, causing performance problems. If you only need the first element, First is the correct method. Conversely, using First when you need to verify uniqueness can hide data integrity issues.
A related issue is using SingleOrDefault and then checking for null or default without considering that it can still throw if multiple elements exist. The exception from multiple elements is not suppressed by the OrDefault suffix; only the empty case is handled.
A Practical Example: Parsing Configuration Values
Consider a configuration file that maps feature flags to boolean values. Each flag should appear exactly once. If a flag is duplicated, that is a configuration error. Using Single to retrieve the value will catch the duplication. This example assumes lines such as feature:enableLogging=true.
var configLines = File.ReadAllLines("app.config");
var featureFlags = configLines
.Where(line => line.StartsWith("feature:"))
.Select(line => line.Split('='))
.Select(parts => (Key: parts[0].Substring("feature:".Length), Value: bool.Parse(parts[1])))
.ToList();
bool enableLogging = featureFlags.Single(flag => flag.Key == "enableLogging").Value;If the configuration file contains feature:enableLogging=true twice, Single will throw, alerting you to the duplicate. If you used First, the second entry would be silently ignored. This is a case where the stricter behavior of Single provides valuable validation.
When you do not need uniqueness, First is the pragmatic choice. For instance, if you are reading a list of error messages and you only want to display the first one, First avoids the overhead of checking for duplicates.
var errors = GetErrors();
string firstError = errors.FirstOrDefault() ?? "No errors";The choice between Single and First ultimately depends on the invariant you want to enforce. Single enforces a uniqueness constraint at runtime, while First optimizes for the common case of retrieving the first match. By matching the method to the actual requirement, you make your code both clearer and more robust.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.