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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:10:42+00:00 2026-06-10T04:10:42+00:00

In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that FYI we are highly likely to

  • 0

In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that “FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers” with regards to how the foreach loops uses the variable.

In the next version each time you run through the “foreach” loop we will generate a new loop variable rather than closing over the same variable every time. This is a “breaking” change but in the vast majority of cases the “break” will be fixing rather than causing bugs.

I have not been able to find anything indicating that this change has been made yet. Is there any indication that this is how the foreach loop will work in C# 5?

  • 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-10T04:10:43+00:00Added an answer on June 10, 2026 at 4:10 am

    This is a change to the C# language, not the .NET framework. Therefore, it only affects code compiled under C# 5.0, regardless of the .NET framework version on which that code will execute.

    C# 5.0

    Section 8.8.4 of the specification makes it clear that this change has been made. Specifically, page 249 of the C# 5.0 specification states:

    foreach (V v in x) embedded-statement
    

    is then expanded to:

    {
        E e = ((C)(x)).GetEnumerator();
        try {
            while (e.MoveNext()) {
                V v = (V)(T)e.Current;
                embedded-statement
            }
        }
        finally {
            … // Dispose e
        }
    }
    

    And later:

    The placement of v inside the while loop is important for how it is
    captured by any anonymous function occurring in the
    embedded-statement.

    C# 4.0

    This change to the specification is clear when comparing with the C# 4.0 specification which states (again, in section 8.8.4, but this time, page 247):

    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 that the variable v is declared outside the loop instead of inside, as it is with C# 5.0.

    Note

    You can find the C# specification in the installation folder of Visual Studio under VC#\Specifications\1033. This is the case for VS2005, VS2008, VS2010 and VS2012, giving you access to specifications for C# 1.2, 2.0, 3.0, 4.0 and 5.0. You can also find the specifications on MSDN by searching for C# Specification.

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

Sidebar

Related Questions

Given this answer: https://stackoverflow.com/a/4263048 Which is excellent, how can we be sure that out
Using the code from this answer: https://stackoverflow.com/a/9744961/514773 I've noticed that any time I enter:
I found an answer that almost solves my problem: https://stackoverflow.com/a/5717191/1065546 This answer demonstrates how
I want to do the same thing that is describe in this answer https://stackoverflow.com/a/1309096/1547704
I have seen this answer https://stackoverflow.com/a/9243472/563381 And while it works well visual as soon
I found this answer to this question https://stackoverflow.com/a/7244888/1473523 , but in my situation am
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
I am using the script from this answer https://stackoverflow.com/a/1681410/22 to insert a launch application
Based on the answer I received on this question ( https://stackoverflow.com/questions/1911969/... ) I have
I try to use wilcards in Android.mk with help of this answer https://stackoverflow.com/a/8459242/1039175 But

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.