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 8454781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:07:01+00:00 2026-06-10T12:07:01+00:00

Is there a significant complexity difference between these two implementation or does the compiler

  • 0

Is there a significant complexity difference between these two implementation or does the compiler optimize it anyway?

Usage:

for(int i = 0; i < int.MaxValue; i++)
{
    foreach(var item in GoodItems)
    {
        if(DoSomethingBad(item))
           break; // this is later added.
    }
}

Implementation (1):

public IEnumerable<T> GoodItems
{
   get { return _list.Where(x => x.IsGood); }
}

Implementation (2):

public IEnumerable<T> GoodItems
{
   get { foreach(var item in _list.Where(x => x.IsGood)) yield return item; }
}

It appears that IEnumerable methods should always be implemented using (2)? When is one better than the other?

  • 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-06-10T12:07:03+00:00Added an answer on June 10, 2026 at 12:07 pm

    Internally, the first version gets compiled down to something that looks like this:

    public IEnumerable<T> GoodItems
    {
        get
        {
            foreach (var item in _list)
                if (item.IsGood)
                    yield return item;
        }
    }
    

    Whereas the second one will now look something like:

    public IEnumerable<T> GoodItems
    {
        get
        {
            foreach (var item in GoodItemsHelper)
                yield return item;
        }
    }
    
    private IEnumerable<T> GoodItemsHelper
    {
        get
        {
            foreach (var item in _list)
                if (item.IsGood)
                    yield return item;
        }
    }
    

    The Where clause in LINQ is implemented with deferred execution. So there’s no need to apply the foreach (...) yield return ... pattern. You’re making more work for yourself, and potentially for the runtime.

    I don’t know if the second version gets jitted to the same thing as the first. Semantically, the two are distinct in that the first does a single round of deferred execution while the second does two rounds. On those grounds I’d argue that the second would be more complex.

    The real question you need to ask is: When you’re exposing the IEnumerable, what guarantees are you making? Are you saying that you want to simply provide forward iteration? Or are you stating that your interface provides deferred execution?

    In the code below, my intent for is to simply provide forward enumeration without random access:

    private List<Int32> _Foo = new List<Int32>() { 1, 2, 3, 4, 5 };
    
    public IEnumerable<Int32> Foo
    {
        get
        {
            return _Foo;
        }
    }
    

    But here, I want to prevent unnecessary computation. I want my expensive computation to be performed only when a result is requested.

    private List<Int32> _Foo = new List<Int32>() { 1, 2, 3, 4, 5 };
    
    public IEnumerable<Int32> Foo
    {
        get
        {
            foreach (var item in _Foo)
            {
                var result = DoSomethingExpensive(item);
                yield return result;
            }
        }
    }
    

    Even though both versions of Foo look identical on the outside, their internal implementation does different things. That’s the part that you need to watch out for. When you use LINQ, you don’t need to worry about deferring execution since most operators do it for you. In your own code, you may wish to go with the first or second depending on your needs.

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

Sidebar

Related Questions

i was wondering if there is any significant difference between consuming .asmx services with
is there some significant difference between PHP 5.4 and 5.2.13 for the following code?
Is there a significant(or even any) difference between 'same' and 'eq' in EasyMock?
Is there any significant performance problems between the two code snippets ? User user
Are there any significant performance differences these two blocks of code? var element =
Is there a significant difference in postgresql's execution, performance, or logic between SELECT users.*
Is vm.runInNewContext considered black magic like eval ? Is there a significant performance difference
Is there a significant difference if I construct a jQuery object around an element
Is there any significant difference in performance when you call [someObject performSelector:@selector(testMethod:) withObject:anotherObject]; vs
I'm currently working on a class that calculates the difference between two objects. I'm

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.