using System; using System.Collections.Generic; using System.Text; namespace interfaceDemo { //委託的定義 // [訪問修飾符] delegate 數據類型 委託名(參數列表....) //1、定義委託 //2.委託的實例化 //3.委託的調用,實際上是將委託指向已經實現的某個方法 //注意:調用的方法必須返回類型和定義的委託返回類型一致 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(); } } }