Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why different versions of Visual Studio will output different result of the same code? [duplicate]

I use the .NET 4 (not .NET 4.5 or any other version of the framework!)

Why different versions of Visual Studio will output different result of the same code using the SAME .NET Framework?

I have the following

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

    foreach (var v in values) {
        funcs.Add(() => v * 10);
    }

    foreach (var f in funcs) {
        Console.WriteLine(f());
    }

    Console.ReadKey();
}

In Visual Studio 2013 the output is 10 20 30 40 50 (Target .NET v == 4).
In Visual Studio 2010 the output is 50 50 50 50 50 (Target .NET v == 4).

Where is the problem? How to identify the C# (not the .NET!) version used by each Studio for the .NET 4

C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /?
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>csc /?
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5

C:\Program Files (x86)\Microsoft Visual Studio 12.0>csc /?
Microsoft (R) Visual C# Compiler version 12.0.30110.0
for C# 5

EDIT

Can I say that

VS 2010 == C# 4
VS 2013 == C# 5

and this independently of the target framework of the concrete solution?

like image 622
serhio Avatar asked Apr 02 '14 13:04

serhio


People also ask

Can you have multiple versions of Visual Studio?

The term side-by-side means that you can install and maintain multiple versions of a product on the same computer. For VSPackages, that means a user can have several Visual Studio versions installed on the same computer.

Can I install VS 2015 after 2019?

Yes. You can have many different versions of Visual Studio installed on a single machine under a single user account, without worrying about them interfering with each other. This has been true for many years. I have one development system with VS 2005, 2008, 2010, 2012, 2013, 2015, 2017, and 2019 installed on it.

Can I install both Visual Studio 2022 and 2019?

You can install Visual Studio on a computer that has an earlier or later version of Visual Studio already installed.


1 Answers

From Eric Lippert's blog post:

In C# 5, the loop variable of a foreach will be logically inside the loop, and therefore closures will close over a fresh copy of the variable each time.

And a quote from MSDN:

Visual Studio 2010 will not let you develop using C# 5. The new C# 5 language features are part of the compiler, and will be included in the Visual Studio 2012 compiler. Even if you install .NET 4.5, this will not let you take advantage of the new features of the language (such as async/await), as these require a new compiler to use.

VS2013 works with only C# 5.0 compiler and you can target various .NET frameworks. Because of this, you use get C# 5.0 features like async/await and still target .NET 4.0.

like image 95
Ofiris Avatar answered Oct 20 '22 01:10

Ofiris