Delegate void d(int x) delegate void D(int x); class C { public static void M1(int i) {...} public void M2(int i) {...} } class Test { static void Main() { D cd1 = new D(C.M1); // static method Test t = new C(); D cd2 = new D(t.M2); // instance method D cd3 = new D(cd2); // another delegate } }
public delegate int MathOp(int i,int j);//定义委托 class DelegateTest { public static int add(int i, int j) {//方法 return i + j; } public static int Mutiply(int num1, int num2) {//方法 return num1 * num2; } static void Main(string[] args) { MathOp mo = new MathOp(add);//委托的实例化,指向add方法 MathOp maOp = new MathOp(Mutiply);//委托的实例化,指向Mutiply方法 Console.WriteLine(mo(10, 20));//委托的调用 Console.WriteLine(maOp(4, 5));//委托的调用 Console.ReadLine(); } } }
原文地址:http://blog.csdn.net/han_yankun2009/article/details/25919565