委托就像一个占位符"{0}",等待传入一个具体的方法
委托又好像只定义了一个方法的接口,等待具体的实现
namespace ConsoleApplication1 { //c#1定义委托 public delegate int CalculatorDelegate(int a, int b); //c#2支持泛型 public delegate TResult MyFunc<T1, T2, TResult>(T1 a, T2 b); class Program { static void Main(string[] args) { Calculator cal = new Calculator(); //c#1 CalculatorDelegate calculatorDelegate1 = new CalculatorDelegate(cal.Add); //c#2不需要new CalculatorDelegate calculatorDelegate2 = cal.Add; //c#2支持匿名方法 MyFunc<int, int, int> myFun2 = delegate (int a, int b) { return a + b; }; //c#3支持lambda MyFunc<int, int, int> myFun3 = (a, b) => a + b; int c = calculatorDelegate1(1, 2); } } public class Calculator { public int Add(int a, int b) { return a + b; } } }