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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:44:13+00:00 2026-05-11T19:44:13+00:00

Take the code below, adapted from this question : //Borrowed from another question because

  • 0

Take the code below, adapted from this question:

//Borrowed from another question because its a simpler example of what happened to me.
IEnumerable<char> query = "Not what you might expect";
foreach(char vowel in "aeiou")
{
     query = query.Where(c => c != vowel);
}
foreach (char Output in query)
{
    System.Out.WriteLine(Output);
}

This only removes the ‘u’ from the query char collection. The core issue has something to do with the fact that the c variable in the Where clause isn’t evaluated until the second foreach. My question is:

1) Why would the delegate generated by the first foreach not capture each value of c as it is built up? Is there some situation I’m unaware of where that is not the desired behavior?

2) If its not capturing the value of c, how is that value even still in scope in the second foreach when the query is actually run? It would seem to me that if its not storing the values of the variables being passed in, then trying to resolve the statement for the second foreach would fail because the the variable c is clearly out of scope.

I don’t understand how it is that ‘use the last value we saw on this variable back when it was in scope’ was a good design decision for this circumstance, and was hoping someone could shed some light on the subject.

  • 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-11T19:44:14+00:00Added an answer on May 11, 2026 at 7:44 pm

    It is capturing vowel; try:

    foreach(char vowel in "aeiou") {
         char tmp = vowel;
         query = query.Where(c => c != tmp);
    }
    

    Jon has a list of related posts here.

    As for why… foreach is defined (in ECMA 334v4 §15.8.4) as:

    A foreach statement of the form `foreach (V v in x) embedded-statement` is then expanded to:
    
    {
      E e = ((C)(x)).GetEnumerator();
      try {
        V v;
        while (e.MoveNext()) {
          v = (V)(T)e.Current;
          embedded-statement
        }
      }
      finally {
        … // Dispose e
      }
    }
    

    Note the V is outside the while – this means it is captured once only for the foreach. I gather that the C# team have, at times, doubted the wisdom of this change (it was inside the while in the C# 1.2 spec, which would have avoided the problem completely). Maybe it’ll change back eventually, but for now it is being true to the specification.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • 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
  • Editorial Team
    Editorial Team added an answer In XSLT, as a pure language with no side effects,… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer You can get an array of keys with allKeys (undefined… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Ok so I figured it out myself (sorta) so I… May 11, 2026 at 11:49 pm

Related Questions

In the code below, why it is that when I take the address of
I would suspect the code below to output: (I am a SmartForm object and
I'm trying to write a vs code snippet that will take the selected and
I'm starting to understand XAML data binding and using the DataTemplate and it's pretty

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.