找出回调循环里的 bug

来自 委托与事件
C# 14 / .NET 10 进阶 4分钟 找出 1处问题

找出为什么循环结束后,每个回调都会输出同一个值。

csharp
using System;
using System.Collections.Generic;

var callbacks = new List<Action>();
for (int i = 0; i < 3; i++)
    callbacks.Add(() => Console.WriteLine(i));

foreach (Action callback in callbacks)
    callback();
在试验场中打开
报告错误