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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Lambda 只是一个委托,它是一个匿名函数,您可以创建该函数以供以后执行。
Lambda 表达式是表达式树形式的未编译委托,您可以在编译和执行之前对其进行操作。
http://msdn.microsoft.com/en-us/library/bb397687。 ASPX
A Lambda is simply a delegate, its an anonymous function that you can create for later execution.
A Lambda Expression is an uncompiled delegate in the form of an Expression Tree that you can manipulate before compiling and executing.
http://msdn.microsoft.com/en-us/library/bb397687.aspx
也许我有点简单化了,但是,如果我是你,首先我会认为 lambda 是一种通过删除嵌套 foreach 循环或前 n 个元素等内容来缩短代码的好方法。
因此,如果您在酒店周围寻找一些便宜的房间,您可以(假设 IEnumerable 中的酒店):
一旦开始点击,您就可以转向更复杂的东西,这是一种随机方法,我可以在我当前的项目中找到使用lambdas(可能是从其他地方偷来的!):
Perhaps I'm being a bit simplistic, but, if I were you, to start with I'd just consider lambdas as a nice way to shorten code by removing things like nested foreach loops or top n elements.
So if you're running round hotels to find some with cheap rooms you could (assuming hotels in IEnumerable):
Once this starts to click you can move onto something more complex, this is a random method that I can find in my current project using lambdas (probably nicked from somewhere else!):
lambda 表达式用于创建匿名函数。这里将匿名函数分配给委托变量:
然后您可以使用委托来调用该函数:
通常使用 lambda 表达式将委托发送给方法,例如将委托发送给
ForEach
方法,以便为列表中的每个元素调用它:A lambda expression is used to create an anonymous function. Here an anonymous function is assigned to a delegate variable:
You can then use the delegate to call the function:
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:我不久前发表了一篇文章,希望对您有所帮助: http: //www.dontcodetired.com/blog/?tag=/lambda+表达式
I did a post a while back which I hope may be of some use: http://www.dontcodetired.com/blog/?tag=/lambda+expressions