委托真的不好理解,发个帖子共勉一下!
using System;
class MyClass
{
public delegate int ArithOp(int a,int b); //声明一个委托ArithOp
public int DoOp(ArithOp ar)
{
int n=3,m=4;
return ar(n,m); //3,4赋给了委托的a,b
}
}
class Test
{
public static int AddOp(int a,int b)
{
return a+b;
}
public static int SubOp(int a,int b)
{
return a-b;
}
public static void Main()
{
MyClass.ArithOp add=new MyClass.ArithOp(AddOp);
MyClass.ArithOp sub=new MyClass.ArithOp(SubOp);
MyClass m=new MyClass();
int r1=m.DoOp(add);
int r2=m.DoOp(sub);
Console.WriteLine("r1={0},r2={1}",r1,r2);
}
}