
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 7 namespace ConsoleApplication1 8 { 9 10 delegate void EatDelegate(string food); 11 12 class Program 13 { 14 static void JimEat(string food) 15 { 16 Console.WriteLine("Jim eats " + food); 17 } 18 static void DavidEat(string food) 19 { 20 Console.WriteLine("David eats " + food); 21 } 22 static void TimEat(string food) 23 { 24 Console.WriteLine("Tim eats " + food); 25 } 26 27 static void Main(string[] args) 28 { 29 EatDelegate jim = new EatDelegate(JimEat); 30 EatDelegate david = new EatDelegate(DavidEat); 31 EatDelegate tim = new EatDelegate(TimEat); 32 jim("Apple"); 33 david("Apple"); 34 tim("Apple"); 35 36 // 委托链 37 EatDelegate ed; 38 ed = jim + tim + david; 39 40 41 foreach(EatDelegate e in ed.GetInvocationList()) 42 { 43 Console.WriteLine(e.Method.Name); 44 } 45 46 ed("apple"); 47 48 ed -= tim; 49 50 ed("orange"); 51 52 // 匿名方法 53 EatDelegate ed1 = null; 54 ed1 += delegate (string food){ Console.WriteLine("A eats " + food);}; 55 ed1 += delegate(string food) { Console.WriteLine("B eats " + food); }; 56 ed1.Invoke("pineapple"); 57 58 Console.ReadLine(); 59 } 60 } 61 }
委托的本职就是一个Class,任何可以声明Class的地方都可以声明委托。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 7 namespace ConsoleApplication1 8 { 9 10 delegate void EatDelegate(string food); 11 12 class Program 13 { 14 static void JimEat(string food) 15 { 16 Console.WriteLine("Jim eats " + food); 17 } 18 static void DavidEat(string food) 19 { 20 Console.WriteLine("David eats " + food); 21 } 22 static void TimEat(string food) 23 { 24 Console.WriteLine("Tim eats " + food); 25 } 26 27 static void Main(string[] args) 28 { 29 EatDelegate jim = new EatDelegate(JimEat); 30 EatDelegate david = new EatDelegate(DavidEat); 31 EatDelegate tim = new EatDelegate(TimEat); 32 jim("Apple"); 33 david("Apple"); 34 tim("Apple"); 35 36 // 委托链 37 EatDelegate ed; 38 ed = jim + tim + david; 39 40 41 foreach(EatDelegate e in ed.GetInvocationList()) 42 { 43 Console.WriteLine(e.Method.Name); 44 } 45 46 ed("apple"); 47 48 ed -= tim; 49 50 ed("orange"); 51 52 // 匿名方法 53 EatDelegate ed1 = null; 54 ed1 += delegate (string food){ Console.WriteLine("A eats " + food);}; 55 ed1 += delegate(string food) { Console.WriteLine("B eats " + food); }; 56 ed1.Invoke("pineapple"); 57 58 Console.ReadLine(); 59 } 60 } 61 }
委托链,可以通过GetInvocationList()获取委托链中的每个委托元素,进而获取每个元素的方法名称,返回值类型等。
还可以通过delegate声明匿名函数。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 7 namespace ConsoleApplication1 8 { 9 10 delegate void EatDelegate(string food); 11 12 class Man 13 { 14 private string name; 15 16 public Man(string name) 17 { 18 this.name = name; 19 } 20 21 public void eat(string food) 22 { 23 Console.WriteLine("{0} eats {1}", name, food); 24 } 25 } 26 27 class Program 28 { 29 30 static void eatTogether(string food, params EatDelegate[] values) 31 { 32 if (values == null) 33 { 34 Console.WriteLine("Party is over."); 35 } 36 else 37 { 38 EatDelegate ed = null; 39 foreach(EatDelegate e in values) 40 { 41 ed += e; 42 } 43 44 ed(food); 45 Console.WriteLine(); 46 } 47 } 48 49 static void Main(string[] args) 50 { 51 Man jim = new Man("Jim"); 52 Man david = new Man("David"); 53 Man tim = new Man("Tim"); 54 Man jun = new Man("Jun"); 55 56 // 动态方法 57 EatDelegate jimED = new EatDelegate(jim.eat); 58 EatDelegate davidED = new EatDelegate(david.eat); 59 EatDelegate timED = new EatDelegate(tim.eat); 60 EatDelegate junED = new EatDelegate(jun.eat); 61 62 EatDelegate ed = null; 63 ed = jimED + davidED + timED + junED; 64 ed("orange"); 65 66 eatTogether("pig", davidED, jimED, junED); 67 68 eatTogether("egg", timED, davidED); 69 70 eatTogether(null, null); 71 72 Console.ReadLine(); 73 } 74 } 75 }
Note: 动态方法。