1using System; 2 3namespace ConsoleApplication1 4{ 5 class Program 6 { 7 delegate string delegateTest(string val); 8 9 //被委托调用的方法 10 static string Test(string str) 11 { 12 str += " 这是传统1.0的方法"; 13 return str; 14 } 15 16 static void Main(string[] args) 17 { 18 //匿名方法写法很简捷 19 delegateTest anonDel = delegate(string str) 20 { 21 str += " 这是C#2.0的匿名方法"; 22 return str; 23 }; //注意"}"后有一个分号 24 25 Console.WriteLine(anonDel("Hello World!")); 26 27 //传统的委托使用方法 28 delegateTest DT = new delegateTest(Test); 29 Console.WriteLine(DT("Hello C#!")); 30 Console.ReadKey(); 31 } 32 } 33}
运行结果:
Hello World! 这是C#2.0的匿名方法
Hello C#! 这是传统1.0的方法
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦