0

This question is related to Why is there a difference in behaviour of an application built in VS 2010 v.s. VS 2012? but in my case I have vs 2010 and 2013 installed (both professional).

According to the answer from the referenced question it is because of the different c# versions. But compiling and running the code from command line :

    class Program
{
    static void Main(string[] args)
    {
        var funcs = new List<Func<int>>();
        var texts = new [] { 1, 2, 3 };


        foreach (var text in texts)
        {
            funcs.Add(delegate { return text; });
        }
        for (int i = 0; i < 3; i++)
            Console.WriteLine(funcs.ToList()[i]());
        Console.ReadKey();
    }
}

With csc /out:My.exe /langversion:4 Program.cs gives me the same 1,2,3 output as with /langversion:5 and /langversion:3

Running with vs 2010 with target framework dot net 4 client profile, gives me 3,3,3 Is there something I am missing?

Community
  • 1
  • 1
Ovidiu Buligan
  • 2,784
  • 1
  • 28
  • 37
  • 4
    [The purpose of the /langversion is only to make the compiler accept specific language constructs. It does not affect the actual behaviour of the compiler](http://stackoverflow.com/questions/29476057/how-to-compile-c-sharp-with-specific-language-version). You need to use a different `csc.exe` to achieve the same behavior as the respective Visual Studio version. – CodeCaster Oct 09 '15 at 08:42
  • What is the output when you move funcs.ToList() outside for loop and assign to a variable and use that variable instead in for loop?? – Viru Oct 09 '15 at 08:42

0 Answers0