using System; delegate void EatDelegate( string food); // 定义了一个委托,委托与预欲委托的方法具有相同的返回值,相同的参数个数与类型 class Man ... { private string name; public Man(string name) ...{ this.name = name; } public void eat(string food) ...{ Console.WriteLine(name + "吃" + food); }} class party ... { static void eatToghter(string food,params EatDelegate[] values)...{ if(values==null) ...{ Console.WriteLine("坐谈会结束"); } else ...{ EatDelegate eatChain=null; foreach(EatDelegate ed in values) eatChain+=ed; eatChain(food); Console.WriteLine(); }} static void Main() ...{ Man ZS = new Man("张三"); Man LS = new Man("李四"); Man WW = new Man("王五"); EatDelegate zs = new EatDelegate(ZS.eat);//代理了zsEat这个方法 EatDelegate ls = new EatDelegate(LS.eat);//代理了lsEat这个方法 EatDelegate ww = new EatDelegate(WW.eat);//代理了wwEat这个方法 Console.WriteLine("张三,李四,王五开party"); eatToghter("青菜", zs, ls, ww); Console.WriteLine("李四出去接电话"); eatToghter("白菜", zs,ww); Console.WriteLine("李四回来了"); eatToghter("苹果", zs, ls, ww);eatToghter(null,null); }}