简体   繁体   English

在VS2012和VS2010上运行相同的程序表现出不同的行为

[英]Running same program on VS2012 and VS2010 exhibits different behavior

I have .net 4.5 installed on my box and I understand that below behavior is related to difference in the way foreach captures closure between .net 3.5 and 4. 我在我的盒子上安装了.net 4.5,我知道以下行为与foreach捕获.net 3.5和4之间的关闭方式的不同有关。

I'd like to better understand why running the same code in both VS 2012 and then with VS2010 shows different output when I compiling using .net Framework 4 in both cases, is it that VS 2012 runs csc with some special flag that cause it to capture closure? 我想更好地理解为什么在VS 2012中运行相同的代码然后在VS2010中显示不同的输出,当我在两种情况下使用.net Framework 4进行编译时,是否VS 2012运行csc并带有一些特殊标志,导致它捕获闭合?

var words = new[] { "foo", "bar", "baz", "beer" };
var actions = new List<Action>();
foreach (string word in words)
{
    actions.Add(() => Console.WriteLine(word));
}

actions.ForEach(e => e());

Output: With VS2010 and .net 4, above program yields "beer" 4 times 输出:使用VS2010和.net 4,上述程序产生“啤酒”4次

With VS2012 and .net 4: 使用VS2012和.net 4:

foo bar baz beer foo bar巴兹啤酒

The fix for this was not part of .Net 4, it was part of C# 5. Thus, it is the compiler you are using, rather than the framework version you are targeting, that affects the behavior of this code. 对此的修复不是.Net 4的一部分,它是C#5的一部分。因此,它是您正在使用的编译器,而不是您所针对的框架版本,它会影响此代码的行为。

This is discussed here , and officially declared here . 这将在这里讨论,并在此正式宣布。

It was a kine of bug , that Microsoft fix it in VS2012. 这是一个bug的发现,微软在VS2012中修复它。 i don't know, did Microsoft fix it in VS 2010 in Last SP?. 我不知道,微软是否在最后SP的VS 2010中修复了它? if you test it please inform us. 如果你测试它,请通知我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DeploymentItem在VS2010和VS2012中的行为方式不同 - DeploymentItem behaving differently in VS2010 and VS2012 VS2010和VS2012的两个C#项目文件在同一目录中 - Two C# project files for VS2010 and VS2012 in the same directory 如何在VS2010和VS2012中加载数据库项目? - How to load database project in VS2010 and VS2012? VS2012 OutOfMemoryException与VS2010没有异常的工作 - VS2012 OutOfMemoryException vs. VS2010 working w/o Exception AppFabric DataCacheFactory()初始化在VS2013中挂起,在VS2010和VS2012中正常工作 - AppFabric DataCacheFactory() initialization hangs in VS2013, works fine in VS2010 and VS2012 从vs2010迁移到vs2012后,Linq包含不起作用 - Linq Contains not working after migrate from vs2010 to vs2012 VS2012中VS2010的“测试列表编辑器”功能(更新) - VS2010's Test List Editor functionality in VS2012 (Update) 将项目从VS2010迁移到VS2012后无法继承接口 - can't inherit interface after migrating project from VS2010 to VS2012 从VS2010移至VS2012后,HTML渲染间歇性丢失内容 - After move from VS2010 to VS2012 HTML rendering is intermittently losing content System.Linq Linq查询无法在VS2012中工作,但在VS2010中可以正常工作 - System.Linq Linq queries not working in VS2012 but works fine in VS2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM