𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?

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 …


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

Image description
When to Use First()

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.

Image description
When to Use Single()

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?." Shreyans Padmani | Sciencx - Saturday March 8, 2025, 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/
HARVARD
Shreyans Padmani | Sciencx Saturday March 8, 2025 » 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?., viewed ,<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/>
VANCOUVER
Shreyans Padmani | Sciencx - » 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?. [Internet]. [Accessed ]. Available 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/
CHICAGO
" » 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?." Shreyans Padmani | Sciencx - Accessed . 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/
IEEE
" » 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž?." Shreyans Padmani | Sciencx [Online]. Available: 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/. [Accessed: ]
rf:citation
» 𝐂# π‹πˆππ: 𝐅𝐒𝐫𝐬𝐭() 𝐯𝐬. 𝐒𝐒𝐧𝐠π₯𝐞()β€Š-β€Šπ—ͺ𝐑𝐚𝐭’𝐬 𝐭𝐑𝐞 πƒπ’πŸπŸπžπ«πžπ§πœπž? | Shreyans Padmani | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.