你可以解释lambda表达式吗?

编程入门 行业动态 更新时间:2024-10-19 19:51:46
本文介绍了你可以解释lambda表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的不获得 lambda表达式。自从ALGOL的日子开始,我们才开始听到他们的观点,直到最近才发现Python和Ruby变得非常受欢迎。现在C#有 => 语法,我的世界(.NET)中的人越来越多地在谈论lamdba表达式。

I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more.

我已经阅读了关于lambda演算的维基百科文章,但我并不是一个数学家。从实际的角度来看,我真的不了解它。什么时候我会使用lambda表达式?为什么?我如何知道这是我应该在做什么?

I've read the Wikipedia article on the lambda calculus, but I'm not really a math guy. I don't really understand it from a practical perspective. When would I use lambda expressions? Why? How would I know that it's what I should be doing?

你能否以前后格式显示如何解决lambda表达式问题的示例?任何命令式语言都很好,但C#对我来说最容易理解。

Can you show examples of how you would solve problems with lambda expressions, in a before-and-after format? Any imperative language is fine, but C# would be easiest for me to understand.

推荐答案

基本上,就C#而言,lambda表达式是创建代理(或表达式树,但现在让我们放在一边的)的简单方法。

Basically as far as C# is concerned, lambda expressions are an easy way to create a delegate (or an expression tree, but let's leave those aside for now).

在C#1中,我们只能从正常创建委托实例方法。 在C#2中,我们获得了匿名方法。 在C#3中,我们获得了lambda表达式,这就像更简洁的匿名方法。

In C# 1 we could only create delegate instances from normal methods. In C# 2 we gained anonymous methods. In C# 3 we gained lambda expressions, which are like more concise anonymous methods.

当你想表达一些需要一个逻辑的逻辑时,它们特别简洁值并返回一个值。例如,在LINQ的上下文中:

They're particularly concise when you want to express some logic which takes one value and returns a value. For instance, in the context of LINQ:

// Only include children - a predicate var query = dataSource.Where(person => person.Age < 18) // Transform to sequence of names - a projection .Select(person => person.Name);

有一个更全面的讨论与其他方面 - 关于关闭的文章。

There's a fuller discussion of this - along with other aspects - in my article on closures.

更多推荐

你可以解释lambda表达式吗?

本文发布于:2023-11-08 07:35:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568745.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:你可以   表达式   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!