此问题与Why is there a difference in behaviour of an application built in VS 2010 v.s. VS 2012?有关 但在我的情况下,我安装了vs 2010和2013(均为专业)。
根据引用问题的答案,这是因为c#版本不同。但是从命令行编译并运行代码:
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();
}
}
使用 csc /out:My.exe / langversion:4 Program.cs 为我提供与 / langversion相同的 1,2,3 输出: 5 和 / langversion:3
使用目标框架dot net 4客户端配置文件与vs 2010一起运行,给我 3,3,3 有什么我想念的吗?