日期:2014-05-20 浏览次数:20830 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { public static int copy;//[0]这个不是闭包 static void Main() { //定义动作组 List<Action> actions = new List<Action>(); for (int counter = 0; counter < 10; counter++) { copy = counter; actions.Add(() => Console.WriteLine(copy)); } //执行动作 foreach (Action action in actions) action(); } } } //注:Action定义如下: //public delegate void Action();
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main() { int copy;//[1]闭包一 //定义动作组 List<Action> actions = new List<Action>(); for (int counter = 0; counter < 10; counter++) { copy = counter; actions.Add(() => Console.WriteLine(copy)); } //执行动作 foreach (Action action in actions) action(); } } } //注:Action定义如下: //public delegate void Action();
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main() { //定义动作组 List<Action> actions = new List<Action>(); for (int counter = 0; counter < 10; counter++) { int copy;//[1]闭包二 copy = counter; //int copy = counter;//换种写法 actions.Add(() => Console.WriteLine(copy)); } //执行动作 foreach (Action action in actions) action(); } } } //注:Action定义如下: //public delegate void Action();
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main() { //定义动作组 List<Action> actions = new List<Action>(); for (int counter = 0; counter < 10; counter++)//[3]闭包三 { actions.Add(() => Console.WriteLine(counter)); } //执行动作 foreach (Action action in actions) action(); } } } //注:Action定义如下: //public delegate void Action();