Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has foreach's use of variables been changed in C# 5?

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?

like image 828
a_hardin Avatar asked Aug 24 '12 16:08

a_hardin


People also ask

Why for each loop is used in c#?

The foreach loop in C# iterates items in a collection, like an array or a list. It proves useful for traversing through each element in the collection and displaying them. The foreach loop is an easier and more readable alternative to for loop.

How do I change the value of a foreach loop?

The value being iterated cannot be changed, because the foreach loop relies on the IEnumerator interface, and this interface has no method to change the value being enumerated.

Does foreach mutate array C#?

The map() method returns an entirely new array with transformed elements and the same amount of data. In the case of forEach() , even if it returns undefined , it will mutate the original array with the callback .


1 Answers

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.

like image 81
Jeff Yates Avatar answered Sep 22 '22 05:09

Jeff Yates