委托,委托链学习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateTest
{
delegate void myDelegateHandle(string s);
class Program
{
static void Main(string[] args)
{
myDelegateHandle mydel = new myDelegateHandle(A.Print);
myDelegateHandle mydelb = new myDelegateHandle(B.Print);
try
{
mydel += mydelb;
mydel("aaaaa");
mydel -= mydelb;
mydel("uuuuuuuuu");
mydel -= mydel;
mydel("tttt");
Console.Read();
}
catch (NullReferenceException nullEx)
{
Console.WriteLine("Null"+nullEx.Message);
}
finally
{
Console.Read();
}
}
}
class A
{
public static void Print(string msg)
{
Console.WriteLine(msg);
}
}
class B
{
public static void Print(string msg)
{
Console.WriteLine("2222222222222");
}
}
}
声明委托(定义好返回类型、要传递的参数,这是要与指向的函数相一致的)
实例化委托,传递要指向的函数名,此时不加“()”;
直接传递给委托和与已经定义好参数类型一致的参数。
浙公网安备 33010602011771号