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

  • SEARCH
  • Home
  • 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 7182831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:51:18+00:00 2026-05-28T17:51:18+00:00

Is the ever any circumstances, when the following: public IEnumerable<Foo> Foobar (Bar bar) {

  • 0

Is the ever any circumstances, when the following:

public IEnumerable<Foo> Foobar (Bar bar)
{
    List<Foo> result = new List<Foo>()
    foreach(var Qux in bar.QuxSource)
    {
        Foo item;
        ... //a procudure that is intricate enough not to be clearly repressentable with a LINQ method
        result.Add(item);
        ... //The aforementioned inticate proceduce continues. Possiblily adding more items to result
    }
    return result;
}

Is preferable to:

public IEnumerable<Foo> Foobar (Bar bar)
{
    foreach(var Qux in bar.QuxSource)
    {
        Foo item;
        ... //a procudure that is intricate enough not to be clearly repressentable with a LINQ method
        yield return item;
        ... //The aforementioned inticate proceduce continues. Possiblily yielding more items
    }
}

I mean the later is clearly wonderful.
With the glory of defered operation, if i only use Foobar(someBar).First(), it doesn’t have togenerate the all the returned items.

I see the former used alot, particulary by experienced coders. (Who I guess aren’t uptodate with list comprehesions in modern C#).
So is the former patern better undersome cercumstances?
(looking only at library code, targetted at reuse)
I’m thinking maybe when being able to produce the items is dependent on some external resource such as having a file open.
What is the use case foreach?
I’m guess it

  • 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. Editorial Team
    Editorial Team
    2026-05-28T17:51:19+00:00Added an answer on May 28, 2026 at 5:51 pm

    Your example use case is correct, but more generally you’d probably want to adopt the former pattern if:

    • The source makes use of a scarce resource and you need to guarantee that the enumerator will be disposed (if you were to begin enumeration on the latter approach and stop in the middle without disposing the enumerator, it would be left open)
    • The source should be enumerated as quickly as possible
    • You must guarantee that the entire source is evaluated
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a general rule, are there ever any circumstances in which it's acceptable for
I've never had the need to really ever use any of the .NET Data
this is the weirdest error I've ever got on Rails. Any idea what this
Ever since I installed emacs on a new machine I have seen an ugly
have you ever created any custom action on your jqGrid? looking at this example
Has anybody ever had any issues with transitioning the opacity of an SVG text
Has anyone ever found any lightbox type javascript / css code that can display
Does anybody has ever created any class consisting of functions to deal with web
Short version Is there a way to prevent R from ever using any virtual
first post ever in any forum with regard to programming... i usually just search

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.