51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
|
|
namespace Lesson15_lambda表达式练习
|
||
|
|
{
|
||
|
|
class Program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
a1()();
|
||
|
|
a2()();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Action a1()
|
||
|
|
{
|
||
|
|
Action action = null;
|
||
|
|
for (int i = 1; i <= 10; i++)
|
||
|
|
{
|
||
|
|
int index = i;
|
||
|
|
action += delegate()
|
||
|
|
{
|
||
|
|
Console.WriteLine(index);
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return action;
|
||
|
|
}
|
||
|
|
public static Action a2()
|
||
|
|
{
|
||
|
|
Action action = null;
|
||
|
|
for (int i = 1; i <= 10; i++)
|
||
|
|
{
|
||
|
|
int index = i;
|
||
|
|
action += () => Show(index);
|
||
|
|
}
|
||
|
|
return action;
|
||
|
|
}
|
||
|
|
public static Action a3()
|
||
|
|
{
|
||
|
|
Action action = null;
|
||
|
|
for (int i = 1; i <= 10; i++)
|
||
|
|
{
|
||
|
|
int index = i;
|
||
|
|
action += () => Show(index);
|
||
|
|
}
|
||
|
|
return action;
|
||
|
|
}
|
||
|
|
static void Show(int x)
|
||
|
|
{
|
||
|
|
Console.WriteLine(x);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|