除了我们自定义委托,系统给我们提供了内置委托类型:Action、Func、Predicate等目的是减少自定义委托的必要。
Action是一个没有参数,没有返回类型的委托。
Action还有16个泛型重载:
Action<T>
Action<T1,T2>
Action<T1....T15,T16>
源代码:
/***
*Action委托
*目的:为了简化委托的使用
*
* 1)委托的声明
* 2)委托的注册
* 3)委托的调用
*
*
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 学习Action委托
{
class Program
{
Action actHandler; //委托的声明
public Program()
{
//委托注册
actHandler += Test1;
actHandler += Test2;
}
public void Test1()
{
Console.WriteLine("Test1");
}
public void Test2()
{
Console.WriteLine("Test2");
}
//委托的调用
public void DisplayInfo()
{
actHandler();
}
static void Main(string[] args)
{
Program obj = new Program();
obj.DisplayInfo();
}
}
}