Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <h2>C# 5.0</h2> <p>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:</p> <pre><code>foreach (V v in x) embedded-statement </code></pre> <blockquote> <p>is then expanded to:</p> </blockquote> <pre><code>{ E e = ((C)(x)).GetEnumerator(); try { while (e.MoveNext()) { V v = (V)(T)e.Current; embedded-statement } } finally { … // Dispose e } } </code></pre> <p>And later:</p> <blockquote> <p>The placement of v inside the while loop is important for how it is captured by any anonymous function occurring in the embedded-statement.</p> </blockquote> <h2>C# 4.0</h2> <p>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):</p> <pre><code>foreach (V v in x) embedded-statement </code></pre> <blockquote> <p>is then expanded to:</p> </blockquote> <pre><code>{ E e = ((C)(x)).GetEnumerator(); try { V v; while (e.MoveNext()) { v = (V)(T)e.Current; embedded-statement } } finally { … // Dispose e } } </code></pre> <p>Note that the variable <code>v</code> is declared outside the loop instead of inside, as it is with C# 5.0.</p> <h2>Note</h2> <p>You can find the C# specification in the installation folder of Visual Studio under <code>VC#\Specifications\1033</code>. 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 <code>C# Specification</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload