using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习5 { // public delegate void GreetingDelegate(string name); class Program { public delegate void GreetingDelegate(string nn); public delegate string GreetingDelegate11(string name); private static void EnglishGreeting(string name) { Console.WriteLine("Morning, " + name); } private static void ChineseGreeting(string name) { Console.WriteLine("早上好, " + name); } private static string ChineseGreeting12(string name) { Console.WriteLine("早上好, " + name); return "123"; } //注意此方法,它接受一个GreetingDelegate类型的方法作为参数 //private static void GreetPeople(string name, GreetingDelegate MakeGreeting) //{ // MakeGreeting(name); //} static void Main(string[] args) { GreetingDelegate delegate1; GreetingDelegate11 GreetingDelegate11; delegate1 = EnglishGreeting; // 先给委托类型的变量赋值 delegate1 += ChineseGreeting; // 给此委托变量再绑定一个 GreetingDelegate11 = ChineseGreeting12; ChineseGreeting("哈哈"); // 将先后调用 EnglishGreeting 与 ChineseGreeting 方法 delegate1("Jimmy Zhang"); string xxx = ChineseGreeting12("Jimmy Zhang"); Console.WriteLine("xxx is : " + xxx); Console.ReadKey(); } } }