Action 类似函数指针...
//1. 直接使用函数指针
Action f1 = fun01;
f1();
Action<string, string> f2 = fun02;
f2("ha", "ha");
//2. 回调函数中传函数
callbackFun1(s => Console.WriteLine(s));
callbackFun2((s1, s2) => fun02(s1, s2));
具体代码如下:
//Action : 函数指针
private void button4_Click(object sender, EventArgs e)
{
//1. 智能函数指针
Action f1 = fun01;
f1();
Action<string, string> f2 = fun02;
f2("ha", "ha");
//2. 回调函数中传函数
callbackFun1(s => Console.WriteLine(s));
callbackFun2((s1, s2) => fun02(s1, s2));
Console.ReadLine();
}
static void callbackFun1(Action<string> action)
{
action("callbackFun1");
}
static void callbackFun2(Action<string, string> action)
{
action("Hello World", " callbackFun2");
}
static void fun01()
{
Console.WriteLine("fun01");
}
static void fun02(string str1, string str2)
{
Console.WriteLine(str1 + str2);
}
结果如下:
fun01
haha
callbackFun1
Hello World callbackFun2