Let's say I want to demo to someone about the differences between foreach in C# 4.0 and 5.0.
So I write up my code snippet:
public static void Main()
{
string[] fruits = { "Apple", "Banana", "Cantelope" };
var actions = new List<Action>();
foreach (var fruit in fruits)
{
actions.Add(() => Console.WriteLine(fruit));
}
foreach(var a in actions)
{
a();
}
}
But no matter how I compile it, it always works as it does in 5.0*. I've tried setting the language version in the csproj file (Build -> Advanced -> Language Version) and I've tried just building it on the command line:
csc myProgram.cs /langversion:4
I can't get it to work the "old" way. Any help? Bonus points if you can tell me how to do it on both the command line and Visual Studio.
* For anyone who doesn't know, in C#. <= 4.0 this would print Cantelope Cantelope Cantelope
, while in C# 5.0+ it would (more intuitively) print Apple Banana Cantelope
. Here's a link, and here's another.