C# delegate Action<T> lambda表达式
2017-03-09 17:19
190 查看
转载以记录:http://blog.csdn.net/educast/article/details/7219854
在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。
View Code
在使用 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
相关文章推荐
- C#中的Action<T>、Func<T>委托和Lambda表达式
- Delegate,Action<T>和Func<T>
- C#的委托 Action<>和Func<>
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- [C#] 泛型委派 Action<T> 與 Func<T>
- c# action<> func<> 这2个委托怎么用和理解
- Action<T> Delegate 使用
- C#基础:委托之Action<T>和Func<T>的用法
- C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用
- 提问c# action<> func<> 这2个委托怎么用和理解
- C#_delegate - Pair<T> 静态绑定
- C#基础:委托之Action<T>和Func<T>的用法
- C#基础:委托之Action<T>和Func<T>的用法(转)
- c# action<> func<> 这2个委托怎么用和理解
- [C#] 泛型委派 Action<T> 與 Func<T>
- .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
- C#委托之 Action<T>委托
- C#.NET的Linq查询、lambda、委托:Func<>和Action
- C#的委托 Action<>和Func<>
- C#的Action<>和Func<>