using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate { //1.定义委托 public delegate int MyDelegate(int i, int j); public class Program { //2.申明委托 //3.实例化委托 public static MyDelegate sumDelegate = new MyDelegate(Sum); public static MyDelegate subDelegate = new MyDelegate(Sub); static void Main(string[] args) { //4.把委托对象作为参数 MyFun(sumDelegate); MyFun(subDelegate); //5.使用委托(直接) int q = sumDelegate(5, 4); Console.ReadKey(); } /****************************写入委托的方法***************************************/ public static int Sum(int m, int n) { return m + n; } public static int Sub(int m, int n) { return m - n; } /****************************调用的方法**********************************************/ public static void MyFun(MyDelegate de) { //5.使用委托(通过方法) int sb = de(3, 2); string str = "输出的结果是:"; Console.WriteLine(str + sb.ToString()); } } }