Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 113215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:40:02+00:00 2026-05-11T02:40:02+00:00

I often want to grab the first element of an IEnumerable<T> in .net, and

  • 0

I often want to grab the first element of an IEnumerable<T> in .net, and I haven’t found a nice way to do it. The best I’ve come up with is:

foreach(Elem e in enumerable) {   // do something with e   break; } 

Yuck! So, is there a nice way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:40:03+00:00Added an answer on May 11, 2026 at 2:40 am

    If you can use LINQ you can use:

    var e = enumerable.First(); 

    This will throw an exception though if enumerable is empty: in which case you can use:

    var e = enumerable.FirstOrDefault(); 

    FirstOrDefault() will return default(T) if the enumerable is empty, which will be null for reference types or the default ‘zero-value’ for value types.

    If you can’t use LINQ, then your approach is technically correct and no different than creating an enumerator using the GetEnumerator and MoveNext methods to retrieve the first result (this example assumes enumerable is an IEnumerable<Elem>):

    Elem e = myDefault; using (IEnumerator<Elem> enumer = enumerable.GetEnumerator()) {     if (enumer.MoveNext()) e = enumer.Current; } 

    Joel Coehoorn mentioned .Single() in the comments; this will also work, if you are expecting your enumerable to contain exactly one element – however it will throw an exception if it is either empty or larger than one element. There is a corresponding SingleOrDefault() method that covers this scenario in a similar fashion to FirstOrDefault(). However, David B explains that SingleOrDefault() may still throw an exception in the case where the enumerable contains more than one item.

    Edit: Thanks Marc Gravell for pointing out that I need to dispose of my IEnumerator object after using it – I’ve edited the non-LINQ example to display the using keyword to implement this pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer About four lines up from that line you quoted: Objects… May 11, 2026 at 12:50 pm
  • added an answer Unfortunately I think the cross-threading issues will make data-binding proper… May 11, 2026 at 12:50 pm
  • added an answer It's a valid question but it needs to be a… May 11, 2026 at 12:49 pm

Related Questions

I often want to trigger a certain function just once, but I need to
I often want to define new 'Exception' classes, but need to have an appropriate
I often want to make a multiline function call and reduce it down to
Using Vim I often want to replace a block of code with a block
I am messing around with the Indy 10 supplied with Delphi 2009 and am
I often have data in Excel or text that I need to get into
I've got a problem that keeps coming up with normalized databases and was looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.