由网友(有压力才有动力)分享简介:谁能给我如何使用LAMBDA一个很好的解释,并给出一个很好的例子。我已经看到了,但我不知道它是什么或做。Can anyone give me a good explanation of how to use Lambda and give a good example. I have seen it but I do...
谁能给我如何使用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.
推荐答案
一个拉姆达EX pression用来创建一个匿名函数。在这里,一个匿名函数被分配给一个委托变量:
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);
通常拉姆达EX pressions用于发送一个委托的方法,例如派遣代表到的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));
相关推荐
最新文章