Lambda 解释以及它是什么以及一个很好的例子 [英] Lambda Explanation and what it is as well as a good example

查看:17
本文介绍了Lambda 解释以及它是什么以及一个很好的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个很好的解释如何使用 Lambda 并举一个很好的例子.我见过它,但我不知道它是什么或做什么.

Can anyone give me a good explanation of how to use Lambda and give a good example. I have seen it but I dont know what it is or does.

推荐答案

一个 lambda 表达式用于创建一个匿名函数.这里将匿名函数分配给委托变量:

A lambda expression is used to create an anonymous function. Here an anonymous function is assigned to a delegate variable:

Func<int, int> increase = (a => a + 1);

然后您可以使用委托来调用函数:

You can then use the delegate to call the function:

var answer = increase(41);

通常 lambda 表达式用于向方法发送委托,例如向 ForEach 方法发送委托,以便为列表中的每个元素调用它:

Usually lambda expressions are used to send a delegate to a method, for example sending a delegate to the ForEach method so that it's called for each element in the list:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);

list.ForEach(n => Console.WriteLine(n));

这篇关于Lambda 解释以及它是什么以及一个很好的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆