C# First vs FirstOrDefault: Differences, Usage, and Examples in LINQ

C# First vs FirstOrDefault: Differences, Usage, and Examples in LINQ

First returns the first element in a collection that matches a condition, while FirstOrDefault returns the first matching element or a default value if no match is found.

In LINQ, both First and FirstOrDefault are used to retrieve the first element from a sequence. The key difference is how they behave when no matching element exists. First throws an exception if the collection is empty or no match is found. FirstOrDefault instead returns a default value (such as null for reference types or 0 for integers). Choosing between them depends on whether an empty result is expected and how you want to handle missing data.

When to Use First vs FirstOrDefault?

Use First when:

• You are sure the collection contains at least one matching element
• Missing data should be treated as an error
• You want strict validation behavior

Use FirstOrDefault when:

• The collection may be empty
• Missing data is acceptable
• You want to avoid exceptions
• You need safer null/default handling

How to Use First and FirstOrDefault?

First Example

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 10, 20, 30 };

        int first = numbers.First();
        Console.WriteLine(first);
    }
}

First with Condition

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 10, 20, 30 };

        int result = numbers.First(n => n > 15);
        Console.WriteLine(result);
    }
}

FirstOrDefault Example

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>();

        int result = numbers.FirstOrDefault();
        Console.WriteLine(result);
    }
}

First vs FirstOrDefault Comparison

Feature First FirstOrDefault
Return Value First matching element First element or default value
Empty Collection Throws exception Returns default value
No Match Found Throws exception Returns default value
Safety Less safe Safer for uncertain data
Performance Same as FirstOrDefault Same as First

Advantages and Disadvantages

Method Advantages Disadvantages
First Strict validation, ensures data exists Can throw exceptions if no data exists
FirstOrDefault Safe for empty collections, avoids exceptions May hide missing data issues

Similar and Alternative Options

Option Description When to Use
Single Returns only one element, throws if multiple exist Strict uniqueness required
SingleOrDefault Returns one element or default Optional unique element
Where + First Filters then selects first More control over filtering
ElementAt Access by index When position is known

Common Mistakes

• Using First on an empty collection without validation
• Assuming FirstOrDefault always returns a valid value
• Ignoring null checks when using reference types
• Using First when data may not exist
• Overusing FirstOrDefault and hiding logic errors
• Not applying filters before calling First

Contents related to 'C# First vs FirstOrDefault: Differences, Usage, and Examples in LINQ'

C# Select vs Where LINQ: Differences, Usage, and Examples
C# Select vs Where LINQ: Differences, Usage, and Examples
C# File Read and Write: Complete Guide with Examples and Best Practices
C# File Read and Write: Complete Guide with Examples and Best Practices