C#
LINQ
programming
List
object-oriented

How to get first object out from ListObject using Linq

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

If you want the first object from a List<T> using LINQ, the usual methods are First() and FirstOrDefault(). They look similar, but they differ in how they behave when the sequence is empty, and that difference matters more than the retrieval itself.

Use First() When an Element Must Exist

First() returns the first element in the sequence and throws an exception if the list is empty.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5var numbers = new List<int> { 10, 20, 30 };
6int first = numbers.First();
7Console.WriteLine(first);

This is appropriate when an empty list would represent a bug or an invalid application state.

Use FirstOrDefault() When Empty Is Acceptable

FirstOrDefault() returns the first element if one exists. Otherwise it returns the default value for the type.

csharp
var numbers = new List<int>();
int first = numbers.FirstOrDefault();
Console.WriteLine(first); // 0 for int

For reference types, the default is null. That makes FirstOrDefault() safer for optional lookup patterns, but it also means you need to think about what the default value means in your program.

Filtering Before Taking the First Match

LINQ becomes especially useful when you want the first element matching a condition.

csharp
1var users = new List<User>
2{
3    new User { Id = 1, Name = "Ada" },
4    new User { Id = 2, Name = "Grace" }
5};
6
7User user = users.FirstOrDefault(u => u.Id == 2);
8Console.WriteLine(user?.Name);

This is often clearer than manually writing a loop for one simple query.

Compare with Index Access

If you only need the first element of a list and you already know it is a List<T>, direct index access also works:

csharp
var first = users[0];

That avoids LINQ overhead, but it throws ArgumentOutOfRangeException when the list is empty. In everyday application code, the bigger decision is usually readability and empty-sequence behavior, not micro-optimization.

Choose the Method by Intent

A useful rule is:

  • use First() when absence is exceptional,
  • use FirstOrDefault() when absence is normal and should be handled,
  • use index access when you are working with a concrete list and want direct positional semantics.

That makes the code communicate what the caller expects from the collection.

Single() Is a Different Contract

Developers sometimes confuse First() with Single(). First() means "give me the first match." Single() means "there must be exactly one match." If you only want the first element in a list, Single() expresses a much stronger requirement and will throw if more than one element exists.

Common Pitfalls

  • Using First() on data that can legitimately be empty.
  • Using FirstOrDefault() and then forgetting to handle null or the default value.
  • Treating 0 from FirstOrDefault() on a numeric list as though it proved an element existed.
  • Reaching for LINQ when plain index access is clearer in a tightly controlled list context.
  • Writing a predicate query and assuming a match must exist without checking that assumption.

Summary

  • First() returns the first item and throws if the sequence is empty.
  • FirstOrDefault() returns the first item or the type’s default value.
  • Predicate overloads let you ask for the first matching object, not only the first positional element.
  • The important choice is how your code should behave when no element exists.
  • Good LINQ usage is about expressing intent clearly, not only shortening syntax.
  • Small API choices can carry significant semantic meaning.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.