匿名方法 (C# 2.0) 和 lambda 表达式 (C# 3.0) 之间有什么区别? [英] What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)?

查看:19
本文介绍了匿名方法 (C# 2.0) 和 lambda 表达式 (C# 3.0) 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 2.0 的匿名方法和C# 3.0的lambda表达式有什么区别?

What is the difference between anonymous methods of C# 2.0 and lambda expressions of C# 3.0.?

推荐答案

MSDN匿名方法页面解释了它

在 2.0 之前的 C# 版本中,唯一的声明委托的方法是使用命名方法.C# 2.0 介绍匿名方法和 C# 3.0 和后来,lambda 表达式取代匿名方法作为首选方式编写内联代码.但是,那关于匿名方法的信息本主题也适用于 lambda表达式.有一个案例在匿名方法提供的在 lambda 中找不到功能表达式.匿名方法启用您可以省略参数列表,并且这意味着匿名方法可以转换为委托各种签名.这不是可以使用 lambda 表达式.为了更多信息,特别是关于lambda 表达式,请参阅 Lambda表达式(C# 编程指南).

In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).

关于 lambda 表达式:

lambda 表达式是一个匿名函数,可以包含表达式和语句,可用于创建委托或表达式树类型.所有 lambda 表达式都使用 lambda 运算符 =>,读作goes to".lambda 运算符的左侧指定输入参数(如果有),右侧保存表达式或语句块.lambda 表达式 x => x * x 读作x 到 x 乘以 x".这个表达式可以分配给一个委托类型,如下所示:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

这篇关于匿名方法 (C# 2.0) 和 lambda 表达式 (C# 3.0) 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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