110,545
社区成员
发帖
与我相关
我的任务
分享
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();
static void Main(string[] args)
{
List<Action> actions = new List<Action>();
Action<int> assign = (i) => actions.Add(() => Console.WriteLine(i));
for (int counter = 0; counter < 10; counter++)
{
assign(counter);
}
foreach (Action action in actions) action();
}
for (int counter = 0; counter < 10; counter++)//[3]闭包三
{
actions.Add(() => Console.WriteLine(counter));
}
object o;
lock (o)
{
//to do
}