I'm rereading a part from c# 5.0 in Nutshell about the capturing iteration variables (Page 138) and I have tried to reproduce the code bellow on c# 4.0 and c# 5.0 but with no hope to catch the difference until now
using System;
class Test
{
static void Main()
{
Action[] actions = new Action[3];
int i = 0;
foreach (char c in "abc")
actions[i++] = () => Console.Write(c);
for (int j = 0; j < 3; j++)
{
actions[j]();
}
foreach (Action a in actions) a();
Console.ReadLine();
}
}
Note I have Visual studio 2012(.net 4.0 and 4.5 installed) and I have changed the target framework while trying to reproduce the issue
Update
to explain more the issue the output with c# 4.0 will be different from c# 5.0
I Know this may be less useful due to update to a recent version of C# Compiler
can anyone enlighten me on how to reproduce this issue ?
ANSI C and ISO C This version of the language is often referred to as ANSI C, Standard C, or sometimes C89. In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90.
So, there were standards before and after ANSI C. Let's continue with a discussion of all the five different standards of C — K&R C, ANSI C, C99, C11 and Embedded C. For the purposes of our discussion, the compiler used is the gcc C compiler from the GNU Compiler Collection (GCC).
C17 is the informal name for ISO/IEC 9899:2018, the most recent standard for the C programming language, prepared in 2017 and published in June 2018. It replaced C11 (standard ISO/IEC 9899:2011). C17 will be superseded by C2x.
C. C is yet another one among the oldest programming languages that are still in use in 2022 because of its ease to develop a program. C was developed at Bell Labs by Dennis Ritchie in the early 1970s and has been widely used by various research-oriented industrial, academic, and government organizations.
You can't reproduce it because you are changing the version of the .Net framework but not the compiler. You're always using C# v5.0 which fixes the issue for foreach
loops. You can see the issue with for
loops though:
Action[] actions = new Action[3];
int i = 0;
for (int j = 0; j < "abc".Length; j++)
actions[i++] = () => Console.Write("abc"[j]);
for (int j = 0; j < 3; j++)
{
actions[j]();
}
foreach (Action a in actions) a();
Console.ReadLine();
To use the old compiler you need an old VS version. In this case to see your code break with foreach
you need to test it in VS 2010 (I did locally).
You might want to try to change the language version of the compiler (as xanatos suggested in the comments) but that doesn't use an old compiler. It uses the same compiler but limits you to using specific features:
Because each version of the C# compiler contains extensions to the language specification,
/langversion
does not give you the equivalent functionality of an earlier version of the compiler.
From /langversion (C# Compiler Options)
Because this issue has been fixed in C# compiler 5.0, you can't reproduce it with Visual studio 2012.
You need to use the C# compiler version 4.0 to reproduce the issue which author is trying to explain. With Visual studio 2010 you can reproduce the problem.
Even if you change the Language version in Vs2012, it still won't work. because you're still using the C# 5.0 compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With