-》Lambda表达式
List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
arr.ForEach(new Action<int>(delegate(int a) { Console.WriteLine(a); }));
arr.ForEach(new Action<int>(a => Console.WriteLine(a)));编译器
匿名方法的代码以下:it
delegate(int a) { Console.WriteLine(a); }io
使用lambda表达式的代码以下:编译
a => Console.WriteLine(a)
<1>lambda
a是输入参数,编译器能够自动推断出它是什么类型的,List
若是没有输入参数,能够写成这样:方法
() => Console.WriteLine("ddd")new
<2>return
=>是lambda操做符参数
<3>
Console.WriteLine(a)是要执行的语句。
若是是多条语句的话,能够用{}包起来。
若是须要返回值的话,能够直接写return语句