This content originally appeared on DEV Community and was authored by Shreyans Padmani
If youβve been working with C# and LINQ, you've likely encountered the methods First() and Single(). While they seem similar at first glance β both are used to retrieve a single element from a collection β they have key differences that affect how and when you should use them.
In this post, we'll break down the differences between these two methods, when to use them, and how they behave when faced with certain conditions. Understanding these nuances can help you avoid common pitfalls and make your code more efficient and error-free.
First()
The First() method in LINQ is designed to retrieve the first element from a sequence that satisfies a specified condition. If no condition is provided, First() simply returns the first element of the sequence.
Behavior:
- If no elements are found that match the condition, it will throw an InvalidOperationException.
- If there are multiple matching elements, it will return the first one
Use First() when you want the first element that matches the condition, even if multiple elements meet the criteria. It's ideal when you're interested in the first matching element, and you're okay with multiple results.
Single()
The Single() method is more restrictive. It expects that exactly one element in the collection matches the condition, and if there are none or more than one matching elements, it throws an exception.
Behavior:
- If no matching elements are found, Single() throws an InvalidOperationException.
- If more than one element matches the condition, Single() throws an InvalidOperationException as well.
Use Single() when you are absolutely sure there should be exactly one element that matches the condition. Itβs best suited for situations where you expect uniqueness, such as fetching a user by their unique ID or any other scenario where the result should be a single entity.
Key Differences Between First() and Single()
Multiple Matches:
- First(): Returns the first element that matches the condition, even if there are multiple matches.
- Single(): Throws an exception if there is more than one matching element.
Empty Sequences:
- Both methods throw an InvalidOperationException if no elements match the condition. However, Single() is stricter in that it expects exactly one match. If more than one match exists, it throws an exception.
Use Cases:
- First(): Use when youβre okay with multiple matches but only need the first one (e.g., the first item in a list).
- Single(): Use when you expect exactly one matching result. This is useful when querying for unique elements (e.g., a user by ID).
This content originally appeared on DEV Community and was authored by Shreyans Padmani

Shreyans Padmani | Sciencx (2025-03-08T07:19:37+00:00) π# ππππ: π π’π«π¬π() π―π¬. ππ’π§π π₯π()β-βπͺπ‘ππ’π¬ ππ‘π ππ’ππππ«ππ§ππ?. Retrieved from https://www.scien.cx/2025/03/08/%f0%9d%90%82-%f0%9d%90%8b%f0%9d%90%88%f0%9d%90%8d%f0%9d%90%90-%f0%9d%90%85%f0%9d%90%a2%f0%9d%90%ab%f0%9d%90%ac%f0%9d%90%ad-%f0%9d%90%af%f0%9d%90%ac-%f0%9d%90%92%f0%9d%90%a2%f0%9d%90%a7/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.