委托系列二
委托基本步骤
1. 声明委托
2. 实例化委托,传递的参数和返回值与委托一致
3. 调用委托(Invoke)
无返回值无参数的委托
class Program { public delegate void DelegateGreeting(); public static void Greeting() { Console.WriteLine("早上好"); } static void Main(string[] args) { DelegateGreeting delegateGreeting = new DelegateGreeting(Greeting); delegateGreeting.Invoke(); } }
带参数和返回值的委托
class Program { public delegate int DelegateAdd(int x,int y); public static int Add(int x,int y) { return x + y; } static void Main(string[] args) { DelegateAdd delegateAdd = new DelegateAdd(Add);
// DelegateAdd delegateAdd = Add; Console.WriteLine(delegateAdd.Invoke(1, 2)); ; } }