1.委托对象可使用 + 运算符进行合并。
2. -运算符可用于从合并的委托中移除组件委托。
3.只有相同类型的委托可以被合并。
多播就是 委托1+委托2
internal class Program
{
Action action;// 无参数 无返回值
Func<int> func; // 无参数有返回值
static void Main(string[] args)
{
// 委托的多播
//1.委托对象可使用 "+" 运算符进行合并。
//2."-" 运算符可用于从合并的委托中移除组件委托
//3.只有相同类型的委托可被合并
Program program = new Program();
//委托的多播
program.action = ProgramMothod;
program.action += ProgramMothod1;
program.action += ProgramMothod2;
program.action();
program.action-= ProgramMothod1;
//program.action();
//多播具有返回值的 只会得到最后一个方法的结果
program.func = ProgramMothod3;
program.func += ProgramMothod4;
program.func += ProgramMothod5;
Console.WriteLine(program.func()); // 30
Console.ReadKey();
}
public static void ProgramMothod() {
Console.WriteLine(1);
}
public static void ProgramMothod1()
{
Console.WriteLine(2);
}
public static void ProgramMothod2()
{
Console.WriteLine(3);
}
public static int ProgramMothod3()
{
return 10;
}
public static int ProgramMothod4()
{
return 20;
}
public static int ProgramMothod5()
{
return 30;
}
}
多播具有返回值的 只会得到最后一个方法的结果