C# LINQ First: Syntax, Behavior, and Pitfalls
Learn how to use LINQ First in C# to retrieve the first element from a sequence, handle empty-sequence exceptions, and choose between First and FirstOrDefault.

The First method in LINQ returns the first element from a sequence. It is a common sequence operator, but its behavior when the sequence is empty often surprises developers. This article covers the syntax, empty-sequence behavior, predicate overload, performance characteristics, and related methods such as FirstOrDefault, Single, and Take(1).
How First Works on Sequences
First is an extension method defined in System.Linq.Enumerable for IEnumerable<T>. It returns the first element of the sequence. If the sequence contains no elements, it throws an InvalidOperationException. The method has two overloads: one without a predicate and one that accepts a predicate and returns the first element that satisfies it.
var numbers = new List<int> { 10, 20, 30 };
int first = numbers.First();
Console.WriteLine(first); // 10The method enumerates the sequence and returns the first element it encounters. For a collection that implements IList<T>, the implementation can directly index into the collection instead of enumerating it. For other IEnumerable<T> sources, it uses the enumerator and moves to the first element.
First vs FirstOrDefault
The most common alternative to First is FirstOrDefault. The difference is in the empty-sequence case: FirstOrDefault returns the default value of the type (null for reference types, zero for numeric types) instead of throwing. This makes it suitable when the absence of an element is a valid condition rather than an error.
var empty = new List<string>();
string value = empty.FirstOrDefault();
Console.WriteLine(value == null); // TrueChoosing between them depends on whether an empty sequence represents a failure. If the sequence must contain at least one element, First surfaces that expectation immediately. If the caller can handle an empty result, FirstOrDefault avoids exception overhead and keeps control flow explicit.
Using First with a Predicate
The predicate overload lets you find the first element that satisfies a condition. It throws InvalidOperationException if no matching element exists, just like the parameterless version.
var orders = new List<Order> { /* ... */ };
Order urgent = orders.First(o => o.Priority == Priority.High);This overload is useful when you know a matching element must exist. If the condition can legitimately match zero elements, use FirstOrDefault with the same predicate.
Common Exceptions and How to Avoid Them
First throws ArgumentNullException if the source sequence is null. The more common exception is InvalidOperationException, thrown when the sequence is empty or no element matches the predicate. That exception is a clear signal that the caller's assumption about the data is wrong.
To avoid it, you can call Any() before First, but for many sequences that adds an extra enumeration. A more idiomatic approach is to use FirstOrDefault and then check for the default value. For a reference type, that check is usually a null test:
var account = accounts.FirstOrDefault(a => a.Id == requestedId);
if (account is null)
{
// handle missing account
}For value types, use a nullable type or a sentinel value to distinguish a missing result from a valid default. Also note that if null is a legitimate element in the sequence, a null check cannot distinguish a found null from no match.
Performance and Lazy Evaluation
First does not force the entire sequence to be enumerated. It stops after the first matching element is found. When the source is an IQueryable backed by a database (for example, an Entity Framework query), the provider can translate First into a query that returns one row. If the source is an IEnumerable<T> that performs expensive computation per element, the cost is limited to the elements examined before the first match.
For collections that implement IList<T>, First uses direct indexing, so it runs in O(1) time. For other sequences, it creates an enumerator and calls MoveNext once. The overhead is minimal, but the exception path in First is more expensive than checking a returned value, so FirstOrDefault is often preferred in performance-sensitive code where an empty result is common.
Comparing First, Single, and Take(1)
First should not be confused with Single, which returns the only element of a sequence and throws if the sequence is empty or contains more than one element. Use Single when the sequence must have exactly one element; use First when you only care about the first element regardless of additional ones. Take(1) returns a sequence containing the first element if one exists; if the source is empty, it returns an empty sequence. This is useful when you want to chain further LINQ operations without triggering an exception.
| Method | Empty sequence behavior | Multiple elements behavior |
|---|---|---|
| First | Throws | Returns first |
| FirstOrDefault | Returns default | Returns first |
| Single | Throws | Throws |
| Take(1) | Returns empty sequence | Returns sequence with first item |
The choice depends on the invariant you want to enforce. If the data model guarantees uniqueness, Single is more defensive. If you only need the first record, First is appropriate.
Maintainability and Readability Considerations
Using First in code communicates that the sequence is expected to be non-empty. This is a form of documentation. However, it also couples the method to the data's state. In a codebase where empty sequences are common, FirstOrDefault with an explicit default-value check often reads better because it makes the fallback behavior visible.
When you do use First, consider whether the default exception message is sufficient for debugging. You can wrap the call in a helper method that provides context:
public static T FirstOrThrow<T>(this IEnumerable<T> source, string message)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
foreach (T item in source)
{
return item;
}
throw new InvalidOperationException(message);
}This keeps the intent clear and avoids repeating the same exception handling logic.
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.