最近在看湯姆大叔的JavaScript教程,總結的相當好,可惜自己功力尚淺不能把學到的融會貫通。看過今天大叔發的一篇博文,在js的回調函數中想到了一點關於Action的用法。
發一段簡單不能在簡單的程序
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 string myName = "CC";
6 GetFullInfo(myName,s => Console.WriteLine(s));
7
8 Console.ReadLine();
9
10 }
11
12
13 static void GetFullInfo(string yourname, Action<string> action)
14 {
15 string firstStr = "Welcome to cnblogs ";
16 action(firstStr + yourname);
17 }
18 }
運行后
將不變的邏輯封裝,變化的部分使用委托Action,增加了程序的靈活度!