在C#5.0之前的版本中,如果在foreach循环中的lambda使用循环变量,那么你会发现一些意想不到的现象,例子如下:
var integers = new List<int> { 1, 2, 3, 4 }; var actions = new List<Action>(); foreach (var integer in integers) { actions.Add(() => Console.WriteLine(integer)); } actions.ForEach(action => action());
这段代码的输出不是所期望的
1
2
3
4
而是
4
4
4
4
原因是actions里的lambda表达式捕获的循环变量被覆盖了,而循环变量的最后一个值是4. 解决方法是引入一个局部变量让lambda表达式捕获或者使用Foreach扩展函数:
var action1s = new List<Action>(); foreach (var integer in integers) { var tmpInt = integer; action1s.Add(() => Console.WriteLine(tmpInt)); } action1s.ForEach(action => action());
var action2s = new List<Action>(); integers.ForEach(interger => action2s.Add(() => Console.WriteLine(interger))); action2s.ForEach(action => action());
完整的例子代码和输出结果如图:
class Program { static void Main(string[] args) { Console.WriteLine("foreach (var integer in integers)"); var integers = new List<int> { 1, 2, 3, 4 }; var actions = new List<Action>(); foreach (var integer in integers) { actions.Add(() => Console.WriteLine(integer)); } actions.ForEach(action => action()); Console.WriteLine("foreach (var integer in integers) with local variable"); var action1s = new List<Action>(); foreach (var integer in integers) { var tmpInt = integer; action1s.Add(() => Console.WriteLine(tmpInt)); } action1s.ForEach(action => action()); Console.WriteLine("use ForEach extension method"); var action2s = new List<Action>(); integers.ForEach(interger => action2s.Add(() => Console.WriteLine(interger))); action2s.ForEach(action => action()); Console.WriteLine("done"); Console.Read(); } }
在C#5.0中这个行为得到了纠正,下图是同样程序的运行结果:
时间: 2024-12-28 12:46:12