委托的小例子
class TestDelegate
{
public delegate void CalculateDelegate(int x,int y);
public static void Add(int x,int y)
{
Console.WriteLine(x + y);
}
public static void Substract(int x, int y)
{
Console.WriteLine(x - y);
}
public static void Main()
{
CalculateDelegate myDelegate = new CalculateDelegate(Add);
myDelegate += new CalculateDelegate(Substract); //将多个方法绑定到一个委托变量,在调用一个方法时,可以依次执行所有方法,这种技术叫多播委托。
myDelegate -= new CalculateDelegate(Add); //用+=和-=创建委托链
myDelegate(2, 5); //运行结果:-3
}
}
{
public delegate void CalculateDelegate(int x,int y);
public static void Add(int x,int y)
{
Console.WriteLine(x + y);
}
public static void Substract(int x, int y)
{
Console.WriteLine(x - y);
}
public static void Main()
{
CalculateDelegate myDelegate = new CalculateDelegate(Add);
myDelegate += new CalculateDelegate(Substract); //将多个方法绑定到一个委托变量,在调用一个方法时,可以依次执行所有方法,这种技术叫多播委托。
myDelegate -= new CalculateDelegate(Add); //用+=和-=创建委托链
myDelegate(2, 5); //运行结果:-3
}
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号