您好,登录后才能下订单哦!
自C#3.0开始,就可以使用一种新语法把实现代码赋予委托:Lambda表达式。只要有委托参数类型的地方,就可以使用Lambda表达式。
Lambda表达式的语法比匿名方法简单。如果所调用的方法有参数,且不需要参数,匿名方法的语法就比较简单,因为这样不需要提供参数。
直接上一些例子,读者可以自行运行调试。
static void Main(string[] args) { //1、lambda之hello world. Func<string, string> lambda = param => { return param; }; Console.WriteLine(lambda("hello lambda.")); //2、lambda之参数 Func<double, double, double, double> cube = (double x, double y, double z) => x * y * z; Console.WriteLine(cube(1, 2, 3));//输出6 //更进一步,参数的类型可以省略 Func<double, double, double, double> cude1 = (x, y, z) => x * y * z; Console.WriteLine(cude1(2, 4, 6));//输出48 //3、lambda之调用外部参数 int someValue = 4; Func<double, double, double, double> f = (x, y, z) => x + y + z + someValue; Console.WriteLine(f(1, 2, 3));//1+2+3+4=10 }
在winform编程中,我们经常给某个按钮添加Click事件,最原始的写法是直接双击按钮,生成Click事件的代码,我们就可以直接在里面写方法了。其实这是vs替我们做了很多工作,其中就有在designer.cs文件中,生成了 this.button1.Click += new System.EventHandler(this.button1_Click);这样的代码,事件要注册才能触发。
this.button1.Click += delegate(object ss, EventArgs ee) { MessageBox.Show("hello,this is button1.Click event..."); };
再看看Lambda表达式的用法。
this.button1.Click += (ss, ee) => { MessageBox.Show("hello,this is button1.Click event..."); };
我们再看一下,从匿名方法到Lambda简写的演化过程,借一张图。
从这个演化过程,可以看出C#语言的发展变化过程。
下面详细演示一下Lambda的应用实例。
首先第一个实例我们应用Lambda对一个List<Student>列表进行循环、查找、排序等操作。
首先我们提供一个实体类:
public class Student { public Student(string name, int age) { this.Name = name; this.Age = age; } public string Name { get; set; } public int Age { get; set; } }
然后我们初始化一个List,
//初始化3个Student对象 var students = new List<Student>() { new Student("zhang3",18), new Student("li4",22), new Student("wang5",20) };
//定义1个打印函数 Action<Student> print = student => Console.WriteLine(string.Concat(student.Name, ":", student.Age));
那么,我们就可以先循环输出一下:
students.ForEach(print);//循环打印输出
students.FindAll(student => student.Age > 20).ForEach(print);
students.Sort((f1, f2) => f1.Age.CompareTo(f2.Age)); students.ForEach(print);//年龄从小到大排序后输出
var students = new List<Student>() { new Student("zhang3",18), new Student("li4",22), new Student("wang5",20), new Student("zhao6",20) };
var result = students.GroupBy(x => x.Age).Select(x => string.Concat(x.Key, ":", x.Count()));//先进行分组,再进行投影 result.ToList().ForEach(x => Console.WriteLine(x.ToString()));//循环输出得到结果
Func<Func<int, int, bool>, Func<int[], int[]>> filter = x => new Func<int[], int[]>(y => y.Skip(1).Where(z => x(y[0], z)).ToArray()); Func<int[], int[]> qsort = x => x; Func<int[], int[]> lesser = dt => filter((x, y) => y < x)(dt); Func<int[], int[]> greater = dt => filter((x, y) => y >= x)(dt); qsort = dt => dt.Length > 1 ? qsort(lesser(dt)).Concat(new int[] { dt[0] }).Concat(qsort(greater(dt))).ToArray() : dt; int[] data = { 4, 3, 1, 4, 6, 7, 5, 9, 3, 11, 1, 2, 11 }; var result = qsort(data); result.ToList().ForEach(x => Console.WriteLine(x.ToString()));//排完序后输出
最后提供一下官方的C# Lambda表达式的权威资料。C# Lambda表达式
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
开发者交流群:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。