您的位置:首页 > 编程语言 > C#

C# delegate Action<T> lambda表达式

2017-03-09 17:19 190 查看
转载以记录:http://blog.csdn.net/educast/article/details/7219854

在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。

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  * Bruce
32  * Alfred
33  * Tim
34  * Richard
35  * Bruce
36  * Alfred
37  * Tim
38  * Richard
39  */


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: