方法调用方法的委托小例子

 //定义委托,它定义了可以代表的方法的类型
        public delegate string GreetingDelegate(string name);
        public MethodGoMethod()
        {
            InitializeComponent();
        }
        private static void EnglishGreeting(string name)
        {
            Console.WriteLine( "Morning, " + name);
        }

        private static string ChineseGreeting(string name)
        {
            return "早上好, " + name;
        }

        //注意此方法,它接受一个GreetingDelegate类型的方法作为参数
        private static void GreetPeople(string name,GreetingDelegate MakeGreeting)
        {
            MakeGreeting(name);
        }
        private static void GreetPeople(string name)
        {
            MessageBox.Show("Morning, " + name);
        }
        private void button1_Click(object sender, EventArgs e)
        {


            //List<Dictionary<string, string>> item = new List<Dictionary<string, string>>();
            //Dictionary<string, string> dic = new Dictionary<string, string>();
            //dic.Add("X","Y");
            //item.Add(dic);
            //Dictionary<string, string> dic1 = new Dictionary<string, string>();
            //dic1.Add("X","Z");
            //item.Add(dic);
            //MessageBox.Show(item.Count.ToString());
            GreetPeople("aaaaaaa",ChineseGreeting);
            GreetPeople("asdasd");
        
        }

 

你可能感兴趣的:(方法调用方法的委托小例子)