Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~4 min readC#
#LINQ#C##IEnumerable#FirstOrDefault
Illustration of C# LINQ First method retrieving the first element from a sequence with a magnifying glass.

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.

csharp
var numbers = new List<int> { 10, 20, 30 }; int first = numbers.First(); Console.WriteLine(first); // 10

The 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.

csharp
var empty = new List<string>(); string value = empty.FirstOrDefault(); Console.WriteLine(value == null); // True

Choosing 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.

csharp
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:

csharp
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.

MethodEmpty sequence behaviorMultiple elements behavior
FirstThrowsReturns first
FirstOrDefaultReturns defaultReturns first
SingleThrowsThrows
Take(1)Returns empty sequenceReturns 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:

csharp
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 tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.