Action、Func是C#下两个内置委托;
1.Action委托(都没有返回值):
Action:无参,无返回值;
Action action = new Action(Test); //Action action = Test;
action();
private void Test()
{
Console.WriteLine("This is a Test");
}
Action<T>:有参数T(1~16 个),无返回值;
Action<object> action = new Action<object>(Test); //Action<object> action = Test;
action("Test");
private void Test(object parameter)
{
Console.WriteLine("This is a :" + parameter);
}
2.Func委托(都有返回值):最后一个参数为返回值
Func<T>:无参,返回值为T;
Func<object> func = new Func<object>(Test); //Func<object> func = Test;
Console.WriteLine(func());
private string Test()
{
return "This is a Test";
}
Func<T1,T2,T>:有参数T1,T2(1~16个),返回值为T
Func<object, object> func = new Func<object, object>(Test); //Func<object, object> func = Test;
Console.WriteLine(func("Test"));
private string Test(object parameter)
{
return "This is a " + parameter;
}