我在C#中有以下代码片段:
var actions = new List<Func<int>>();
IEnumerable<int> values = new List<int> { 1, 2, 3 };
foreach (int value in values)
{
actions.Add(() => value * value);
}
foreach (var action in actions)
{
Console.WriteLine(action()); ;
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
它运行正常,但我没有得到我期望的结果.
实际结果
9,9,9
预期结果
1,4,9
为什么我没有得到我期望的结果?