委托理解起来就想c++ 函数指针
我的理解为 : 方法的快捷方式
delegate int AddNumDelegate(int n); class Program { static void Main(string[] args) { AddNumDelegate addndlgt = new AddNumDelegate(Mydelegate.AddNum); Console.WriteLine(addndlgt(15)); Console.ReadLine(); } } class Mydelegate { public static int num = 10; public static int AddNum(int p) { num += p; return num; } }
multicasting delegate 多路委托
delegate int AddNumDelegate(int n);
class Program
{
static void Main(string[] args)
{
AddNumDelegate addndlgt = new AddNumDelegate(Mydelegate.AddNum);
Console.WriteLine(addndlgt(15));
AddNumDelegate addndlgt2 = new AddNumDelegate(Mydelegate.RemoveNum);
Console.WriteLine(addndlgt2(15));
//Mydelegate.num = 0;
AddNumDelegate addndlgtALL = addndlgt + addndlgt2;
Console.WriteLine(addndlgtALL(15));
Console.ReadLine();
}
}
class Mydelegate
{
public static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int RemoveNum(int p)
{
num -= p;
return num;
}
}