为什么不同版本的Visual Studio将相同的code输出不同的结果呢? [英] Why different versions of Visual Studio will output different result of the same code?

查看:317
本文介绍了为什么不同版本的Visual Studio将相同的code输出不同的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 .NET 4 不.NET 4.5 的或任何其他版本的框架!)

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

为什么不同版本的Visual Studio 也同样code。使用输出不同的结果,在同一个.NET框架

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

我有以下

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();
}

在Visual Studio中的 2013 输出 10 20 30 40 50 (目标.NET v == 4)。
在Visual Studio中的 2010 输出 50 50 50 50 50 (目标.NET v == 4)。

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).

问题出在哪里?如何识别 C#(而不是.NET!)由每个版本演播室 .NET 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 \框架\ v4.0.30319> CSC /
  微软(R)的Visual C#编译器版本4.0.30319.33440
  对于Microsoft(R).NET框架4.5

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)的\微软的Visual Studio的 10.0 \ VC> CSC /
  微软(R)的Visual C#编译器版本4 .0.30319.33440
  对于Microsoft(R).NET框架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)的\微软的Visual Studio的 12.0 > CSC /
  微软(R)的Visual C#编译器第12版 .0.30110.0
  对于C#5

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

我可以说,

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

这独立于具体的解决方案的目标框架?

推荐答案

从埃里克利珀的<一个href="http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx">blog帖子:

在C#5,一个foreach的循环变量将是逻辑里面的   回路,因此关闭将关闭过的一个全新副本   变各一次。

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.

和从MSDN报价:

Visual Studio 2010中不会让你开发使用C#5,新的C#5   语言特性是编译器的组成部分,并且将被包含在   在Visual Studio 2012的编译器。即使您安装.NET 4.5,这   不会让你走的语言的新功能   (如异步/等待),因为这些需要新的编译器使用。

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 仅适用 C#5.0 编译器,你可以针对不同的.NET框架。正因为如此,你用得到C#5.0的功能,比如异步/等待,仍然面向.NET 4.0。

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.

这篇关于为什么不同版本的Visual Studio将相同的code输出不同的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆