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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:39:40+00:00 2026-05-29T03:39:40+00:00

Just trying to understand this better. I know this is because of Deffered Execution

  • 0

Just trying to understand this better. I know this is because of Deffered Execution

But what is it causing the method not getting called immediately. This is from EduLinq from JonSkeet.

 public static partial class Enumerable
{
    public static IEnumerable<TSource> Where<TSource>(
        this IEnumerable<TSource> source,
        Func<TSource, bool> predicate)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
        if (predicate == null)
        {
            throw new ArgumentNullException("predicate");
        }

        foreach (TSource item in source)
        {
            if (predicate(item))
            {
                yield return item;
            }
        }
    }
}

This is where I am using it.

List<int> list = new List<int>() { 1, 3, 4, 2, 8, 1 };

var s = list.Where(x => x > 4);

var result = s.ToList();

My question is though Where is a static method on IEnumerable why is it not called on list.where(). But it is called on s.ToList().

I have a breakpoint on Enumerable.Where() it is not hit on s = list.Where(x => x > 4) but the breakpoint is hit on s.ToList()

After I saw the comment from YUCK Why does LINQ have deferred execution? I am adding this to the question.

Please let me know.

  • 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-29T03:39:41+00:00Added an answer on May 29, 2026 at 3:39 am

    The Where method is actually called, but it returns an IEnumerable<T>. This return value is actually a class the compiler implements for you.

    Note that your implementation uses an iterator (it includes yield return …). When this occurs, the compiler changes your method around so that it creates a compiler-generated class, and, as you actually iterate through the IEnumerable<T>, the code you wrote gets executed.

    The first time MoveNext is called, the code up to your first yield return will get executed. The second call will go until the next, etc.

    Calling ToList() enumerates through the entire IEnumerable<T>, which in turn executes your entire method.

    Also – ToList() isn’t required here to have your code execute. You could use a foreach loop:

    foreach(var val in s) 
    {
         // Do something...
    }
    

    Or even execute the calls manually:

    IEnumerable<int> s = list.Where(x => x > 4);
    IEnumerator<int> sEnumerator = s.GetEnumerator(); // Get the enumerator
    
    // This section will cause your code to run, but only until the *first* yield statement...
    if (sEnumerator.MoveNext())
    {
        int val = sEnumerator.Current();
    }
    
    // Without further MoveNext() calls, you won't "finish" the foreach loop...
    
    // This block will do one more "loop" in your foreach, going until the next "yield" (or the end of the method)
    if (sEnumerator.MoveNext())
    {
        int val = sEnumerator.Current();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to understand this a little better. I understand there are many
Am just trying to understand why this exception throws. Cannot implicitly convert type 'System.DateTime?'
I grabbed this code form JCarousel and just trying to understand these lines below.
This might be a naive question. However, I am just trying to understand why
Im trying to understand how class generics work and this bit just doesnt make
Premise: I'm not trying to reinvent the wheel, I'm just trying to understand. Output
I am just trying to understand this: multithread is a way of firing a
This is just a question to help me understand CSS rendering better. Lets say
I am just getting into functional programming and trying to understand when a class/property
I'm just getting to know regular expressions, but after doing quite a bit of

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.