How do I get the first element from an IEnumerableT in .net?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The most common way to get the first element from an IEnumerable<T> in .NET is First() (throws if empty) or FirstOrDefault() (returns default(T) if empty). Both are LINQ extension methods in System.Linq. For performance-critical code with lists, list[0] is fastest but only works with IList<T>. Use FirstOrDefault() as the safe default and First() only when you are certain the collection is non-empty. For nullable reference types in C# 8+, be aware that FirstOrDefault() returns null for empty collections of reference types.
First() — Throws on Empty
Use First() when you expect the collection to always have at least one element. The exception acts as a fail-fast assertion — if the collection is unexpectedly empty, you want to know immediately.
FirstOrDefault() — Returns Default on Empty
FirstOrDefault() is the safe choice when the collection might be empty. For value types, it returns the type's default (0 for int, false for bool). For reference types, it returns null.
FirstOrDefault with Custom Default (.NET 6+)
The .NET 6+ overload lets you specify a fallback value, avoiding the ambiguity of 0 or null as "not found."
ElementAt() and ElementAtOrDefault()
ElementAt(0) is functionally equivalent to First() but is index-based. It is useful when you need an element at a specific position, not just the first.
Direct Indexing (List<T> Only)
Direct indexing is the fastest option because it avoids iterator allocation. However, it only works with types that implement IList<T> (like List<T> and arrays), not with general IEnumerable<T>.
Using Enumerator Manually
Manually using the enumerator avoids LINQ overhead and works with any IEnumerable<T>. This is rarely needed but useful in performance-critical code or when avoiding a LINQ dependency.
Pattern Matching (C# 8+)
Comparison Table
| Method | Empty behavior | Performance | Works with |
First() | Throws exception | O(1) | Any IEnumerable<T> |
FirstOrDefault() | Returns default | O(1) | Any IEnumerable<T> |
list[0] | Throws IndexOutOfRange | O(1) | IList<T> only |
ElementAt(0) | Throws exception | O(1) for lists | Any IEnumerable<T> |
Common Pitfalls
- Using
First()on potentially empty collections:First()throwsInvalidOperationExceptionif the collection is empty. UseFirstOrDefault()when the collection might be empty, or checkAny()before callingFirst(). - Confusing
FirstOrDefault()default with "not found": Forint,FirstOrDefault()returns0both when the first element is0and when the collection is empty. UseFirstOrDefault(-1)(.NET 6+) or checkAny()first to distinguish between "found 0" and "empty." - Multiple enumeration of
IEnumerable<T>: CallingAny()thenFirst()enumerates the sequence twice. If the source is a database query or network stream, this is expensive or incorrect. UseFirstOrDefault()and check fornull/defaultinstead. - Using
Single()whenFirst()is intended:Single()throws if the collection has more than one element. If you just want the first element regardless of collection size, useFirst().Single()is for asserting uniqueness. - Null reference from
FirstOrDefault()on reference types: ForList<string>,FirstOrDefault()returnsnullon an empty list. Accessing properties on the result without a null check causesNullReferenceException. Use the null-conditional operator:result?.Length.
Summary
- Use
FirstOrDefault()as the safe default — it returnsdefault(T)on empty collections - Use
First()when the collection is guaranteed non-empty and you want fail-fast behavior - Use
list[0]for direct index access onList<T>— fastest but requires bounds checking - In .NET 6+, use
FirstOrDefault(fallback)to specify a custom default value - Avoid multiple enumeration — prefer
FirstOrDefault()overAny()+First() - Be careful with value type defaults:
0forint,falseforboolmay be valid values
Related reading
- How do I get the MAX row with a GROUP BY in LINQ query?
- How do I get the path of the assembly the code is in?
- How do I get the path of the assembly the code is in?
- How do I get the title of the current active window using c?
- How do I get Visual Studio to stay on one thread when debugging?
- How do I group Windows Form radio buttons?
- How do I handle Database Connections with Dapper in .NET?
- How do I implement IEnumerableT

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.