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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:50:33+00:00 2026-05-28T06:50:33+00:00

Can someone please explain me what I am missing here. Based on my basic

  • 0

Can someone please explain me what I am missing here. Based on my basic understanding linq result will be calculated when the result will be used and I can see that in following code.

 static void Main(string[] args)
 {
     Action<IEnumerable<int>> print = (x) =>
     {
         foreach (int i in x)
         {
             Console.WriteLine(i);
         }
     };

     int[] arr = { 1, 2, 3, 4, 5 };
     int cutoff = 1;
     IEnumerable<int> result = arr.Where(x => x < cutoff);
     Console.WriteLine("First Print");
     cutoff = 3;
     print(result);
     Console.WriteLine("Second Print");
     cutoff = 4;
     print(result);
     Console.Read();
}

Output:

First Print
1
2
Second Print
1
2
3

Now I changed the

arr.Where(x => x < cutoff); 

to

IEnumerable<int> result = arr.Take(cutoff); 

and the output is as follow.

First Print
1
Second Print
1

Why with Take, it does not use the current value of the variable?

  • 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-28T06:50:34+00:00Added an answer on May 28, 2026 at 6:50 am

    There’s a few different things getting confused here.

    Late-binding: This is where the meaning of code is determined after it was compiled. For example, x.DoStuff() is early-bound if the compiler checks that objects of x‘s type have a DoStuff() method (considering extension methods and default arguments too) and then produces the call to it in the code it outputs, or fails with a compiler error otherwise. It is late-bound if the search for the DoStuff() method is done at run-time and throws a run-time exception if there was no DoStuff() method. There are pros and cons to each, and C# is normally early-bound but has support for late-binding (most simply through dynamic but the more convoluted approaches involving reflection also count).

    Delayed execution: Strictly speaking, all Linq methods immediately produce a result. However, that result is an object which stores a reference to an enumerable object (often the result of the previous Linq method) which it will process in an appropriate manner when it is itself enumerated. For example, we can write our own Take method as:

    private static IEnumerable<T> TakeHelper<T>(IEnumerable<T> source, int number)
    {
      foreach(T item in source)
      {
        yield return item;
        if(--number == 0)
          yield break;
      }
    }
    public static IEnumerable<T> Take<T>(this IEnumerable<T> source, int number)
    {
      if(source == null)
        throw new ArgumentNullException();
      if(number < 0)
        throw new ArgumentOutOfRangeException();
      if(number == 0)
        return Enumerable.Empty<T>();
      return TakeHelper(source, number);
    }
    

    Now, when we use it:

    var taken4 = someEnumerable.Take(4);//taken4 has a value, so we've already done
                                        //something. If it was going to throw
                                        //an argument exception it would have done so
                                        //by now.
    
    var firstTaken = taken4.First();//only now does the object in taken4
                                            //do the further processing that iterates
                                            //through someEnumerable.
    

    Captured variables: Normally when we make use of a variable, we make use of how its current state:

    int i = 2;
    string s = "abc";
    Console.WriteLine(i);
    Console.WriteLine(s);
    i = 3;
    s = "xyz";
    

    It’s pretty intuitive that this prints 2 and abc and not 3 and xyz. In anonymous functions and lambda expressions though, when we make use of a variable we are “capturing” it as a variable, and so we will end up using the value it has when the delegate is invoked:

    int i = 2;
    string s = "abc";
    Action λ = () =>
    {
      Console.WriteLine(i);
      Console.WriteLine(s);
    };
    i = 3;
    s = "xyz";
    λ();
    

    Creating the λ doesn’t use the values of i and s, but creates a set of instructions as to what to do with i and s when λ is invoked. Only when that happens are the values of i and s used.

    Putting it all together: In none of your cases do you have any late-binding. That is irrelevant to your question.

    In both you have delayed execution. Both the call to Take and the call to Where return enumerable objects which will act upon arr when they are enumerated.

    In only one do you have a captured variable. The call to Take passes an integer directly to Take and Take makes use of that value. The call to Where passes a Func<int, bool> created from a lambda expression, and that lambda expression captures an int variable. Where knows nothing of this capture, but the Func does.

    That’s the reason the two behave so differently in how they treat cutoff.

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

Sidebar

Related Questions

Can someone please explain how to obtain this result from the following array? Here
Can someone please explain to me why this simple assignment doesn't work. Here is
Can someone please explain to me how paging works in SmartGWT? I see it
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please explain what the & does in the following: class TEST {
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections
Can someone please explain how to understand a logcat from an android force close.
Can someone please explain the major differences between Scala, Groovy and Clojure. I know

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.