When working with LINQ in C#, retrieving a single element from a collection is a common requirement. Two frequently used methods for this purpose are First() and FirstOrDefault(). Although they appear similar, they behave differently when no matching element is found. Understanding this difference is critical for writing robust and exception-safe .NET applications.
This article explains the functional difference between First() and FirstOrDefault(), their behavior with reference and value types, performance considerations, and practical usage scenarios in real-world ASP.NET Core and .NET applications.
Understanding First() in LINQ
The First() method returns the first element in a sequence that satisfies a specified condition. If no element matches the condition, it throws an InvalidOperationException.
Example without condition:
var numbers = new List<int> { 10, 20, 30 };
var result = numbers.First();
Console.WriteLine(result); // Output: 10
Example with condition:
var numbers = new List<int> { 10, 20, 30 };
var result = numbers.First(n => n > 15);
Console.WriteLine(result); // Output: 20
If no matching element exists:
var numbers = new List<int> { 10, 20, 30 };
var result = numbers.First(n => n > 50); // Throws InvalidOperationException
Because First() throws an exception when no match is found, it should be used only when you are certain that at least one matching record exists in the collection or database result.
Understanding FirstOrDefault() in LINQ
The FirstOrDefault() method also returns the first element that satisfies a condition. However, if no matching element exists, it returns the default value instead of throwing an exception.
For reference types, the default value is null. For value types, the default value depends on the type (e.g., 0 for int, false for bool).
Example with reference type:
var names = new List<string> { "Amit", "Rahul" };
var result = names.FirstOrDefault(n => n == "Vikas");
Console.WriteLine(result == null ? "Not Found" : result);
Example with value type:
var numbers = new List<int> { 10, 20, 30 };
var result = numbers.FirstOrDefault(n => n > 50);
Console.WriteLine(result); // Output: 0
This behavior makes FirstOrDefault() safer in scenarios where empty results are expected.
Difference Between First() and FirstOrDefault()
| Feature | First() | FirstOrDefault() |
|---|
| Return when match found | First matching element | First matching element |
| Return when no match | Throws InvalidOperationException | Returns default value |
| Safe for optional data | No | Yes |
| Suitable for strict validation | Yes | No |
| Common use case | When record must exist | When record may or may not exist |
Behavior in Entity Framework and SQL Queries
In Entity Framework or LINQ to Entities, both First() and FirstOrDefault() translate into SQL queries with TOP (1). However, the difference occurs after execution:
Example in ASP.NET Core using Entity Framework:
var user = _context.Users.FirstOrDefault(u => u.Email == email);
if (user == null)
{
return NotFound();
}
Using First() in this scenario could crash the application if the user does not exist.
Performance Considerations
From a performance perspective, there is no significant difference between First() and FirstOrDefault(). Both stop execution as soon as the first match is found. The difference lies purely in error handling and default return behavior.
However, improper usage of First() in high-traffic production systems may result in avoidable exceptions, which can impact application stability and logging overhead.
When to Use First()
Use First() when:
Business logic guarantees at least one matching record
Absence of data indicates a serious logic failure
You want an exception to be thrown intentionally
Example:
var adminUser = users.First(u => u.Role == "Admin");
If no admin exists, the system should fail fast.
When to Use FirstOrDefault()
Use FirstOrDefault() when:
Data may or may not exist
You need to handle null safely
You are querying user input–based data
Example:
var product = products.FirstOrDefault(p => p.Id == productId);
if (product == null)
{
Console.WriteLine("Product not found");
}
Common Mistakes to Avoid
Using First() without validating data existence
Forgetting to check null when using FirstOrDefault()
Confusing default value of value types with actual valid data
Not handling exceptions properly in production code
Summary
First() and FirstOrDefault() in LINQ both retrieve the first matching element from a collection, but they differ in how they behave when no matching data exists. First() throws an exception, making it suitable for scenarios where data must exist, while FirstOrDefault() returns a default value, making it safer for optional or user-driven queries. Choosing the correct method improves code reliability, enhances error handling in ASP.NET Core and .NET applications, and prevents unnecessary runtime exceptions in production environments.