標籤:ges contract length blog ffffff 分享 引用 stat 而且
轉載以記錄:http://blog.csdn.net/educast/article/details/7219854
在使用 Action<T> 委託時,不必顯式定義一個封裝只有一個參數的方法的委託。以下代碼顯式聲明了一個名為 DisplayMessage 的委託,並將對 WriteLine 方法或 ShowWindowsMessage 方法的引用分配給其委託執行個體。
1 usingSystem; 2 usingSystem.Windows.Forms; 3 delegatevoid DisplayMessage(stringmessage); 4 publicclass TestCustomDelegate{ 5 public static void Main() 6 { 7 DisplayMessage messageTarget; 8 if(Environment.GetCommandLineArgs().Length > 1) 9 messageTarget = ShowWindowsMessage;10 else11 messageTarget = Console.WriteLine; 12 messageTarget("Hello, World!"); 13 } 14 15 private static void ShowWindowsMessage(stringmessage)16 {17 MessageBox.Show(message); 18 }19 }20 View Code
以下簡化了此代碼,它所用的方法是執行個體化 Action<T> 委託,而不是顯式定義一個新委託並將命名方法分配給該委託。
1 usingSystem; 2 usingSystem.Windows.Forms; 3 publicclass TestAction1 4 { 5 publicstatic void Main() 6 { 7 Action<string> messageTarget; 8 if(Environment.GetCommandLineArgs().Length > 1) 9 messageTarget = ShowWindowsMessage;10 else11 messageTarget = Console.WriteLine; 12 messageTarget("Hello, World!"); 13 } 14 15 privatestatic void ShowWindowsMessage(stringmessage)16 {17 MessageBox.Show(message); 18 }19 }View Code
也可以將 Action<T> 委託與匿名方法一起使用。
1 usingSystem; 2 usingSystem.Windows.Forms; 3 4 publicclass TestAnonMethod 5 { 6 publicstatic void Main() 7 { 8 Action<string> messageTarget; 9 if(Environment.GetCommandLineArgs().Length > 1)10 messageTarget = delegate(strings) { ShowWindowsMessage(s); };11 else12 messageTarget = delegate(strings) { Console.WriteLine(s); }; 13 messageTarget("Hello, World!");14 }15 16 privatestatic void ShowWindowsMessage(stringmessage)17 {18 MessageBox.Show(message); 19 }20 }View Code
也可以將 lambda 運算式分配給 Action<T> 委託執行個體。
1 usingSystem; 2 usingSystem.Windows.Forms; 3 4 public class TestLambdaExpression 5 { 6 public static void Main() 7 { 8 Action<string> messageTarget; 9 10 if(Environment.GetCommandLineArgs().Length > 1)11 messageTarget = s => ShowWindowsMessage(s); 12 else13 messageTarget = s => Console.WriteLine(s);14 15 messageTarget("Hello, World!");16 }17 18 private static void ShowWindowsMessage(stringmessage)19 {20 MessageBox.Show(message); 21 }22 }View Code
下面使用 Action<T> 委託來列印 List<T> 對象的內容。 使用 Print 方法將列表的內容顯示到控制台上。 此外還使用匿名方法將內容顯示到控制台上。 請注意該樣本不顯式聲明 Action<T> 變數。 相反,它傳遞方法的引用,該方法採用單個參數而且不將值返回至 List<T>.ForEach 方法,其單個參數是一個 Action<T> 委託。 同樣,在 C# 樣本 中,Action<T> 委託不被顯式地執行個體化,因為匿名方法的簽名匹配 List<T>.ForEach 方法所期望的 Action<T> 委託的簽名。
1 usingSystem; 2 usingSystem.Collections.Generic; 3 4 classProgram 5 { 6 staticvoid Main() 7 { 8 List<String> names = newList<String>(); 9 names.Add("Bruce");10 names.Add("Alfred");11 names.Add("Tim");12 names.Add("Richard");13 14 // Display the contents of the list using the Print method.15 names.ForEach(Print);16 17 // The following demonstrates the anonymous method feature of C#18 // to display the contents of the list to the console.19 names.ForEach(delegate(String name)20 {21 Console.WriteLine(name);22 });23 }24 25 privatestatic void Print(strings)26 {27 Console.WriteLine(s);28 }29 }30 /* This code will produce output similar to the following:31 * Bruce32 * Alfred33 * Tim34 * Richard35 * Bruce36 * Alfred37 * Tim38 * Richard39 */View Code
C# delegate Action<T> lambda運算式