public class Program
{
public delegate void MyDelegate();
public static void Main(string[] args)
{
MyDelegate d = null;
for (int i = 1; i <= 5; i++)
{
MyDelegate tempD = delegate { Console.WriteLine(i); };
d += tempD;
}
d();
}
}
6
6
6
6
6
預期 1~5,實際全為 6。下一篇將詳細解析原因與解決方案。