- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApplication4
- {
- public delegate void EatDelegate(string s);
- public class Man
- {
- private string _Name;
- public Man(string name)
- {
- this._Name = name;
- }
- public void EatFood(string food)
- {
- Console.WriteLine(_Name +"吃"+ food);
- }
- }
- class Party
- {
- /// <summary>
- /// 委托作为参数传递
- /// </summary>
- /// <param name="food">吃什么东西</param>
- /// <param name="edValues">委托类型</param>
- static void EatTogether(string food, params EatDelegate[] edValues)
- {
- if (edValues == null)
- {
- Console.WriteLine("座谈会结束");
- }
- else
- {
- EatDelegate eatChain = null;
- foreach (EatDelegate ed in edValues)
- {
- eatChain += ed;
- }
- eatChain(food);
- Console.WriteLine();
- }
- }
- static void Main()
- {
- Man zs = new Man("张三");
- Man ww = new Man("王五");
- Man ls = new Man("李四");
- EatDelegate edzs = new EatDelegate(zs.EatFood);
- EatDelegate edww= new EatDelegate(ww.EatFood);
- EatDelegate edls= new EatDelegate(ls.EatFood);
- EatTogether("一起吃西瓜", edls, edww, edzs); // 一起吃
- Console.WriteLine("张三出去打电话");
- EatTogether("葡萄", edww, edls);
- EatTogether(null, null);
- Console.ReadLine();
- }
- }
- }
输出结果: