C#Lambda表达式:为什么要使用它们?

本文翻译自:C# Lambda expressions: Why should I use them?

I have quickly read over the Microsoft Lambda Expression documentation. 我已经快速阅读了Microsoft Lambda Expression文档。

This kind of example has helped me to understand better, though: 但是,这种示例有助于我更好地理解:

delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25

Still, I don't understand why it's such an innovation. 不过,我仍然不明白为什么这是一项创新。 It's just a method that dies when the "method variable" ends, right? 这只是一种在“方法变量”结束时消失的方法,对吗? Why should I use this instead of a real method? 为什么要使用此方法而不是实际方法?


#1楼

参考:https://stackoom.com/question/hX5/C-Lambda表达式-为什么要使用它们


#2楼

很多时候,您只在一个地方使用功能,因此制作方法只会使类混乱。


#3楼

It saves having to have methods that are only used once in a specific place from being defined far away from the place they are used. 不必将方法定义为远离特定的使用位置,而不必在特定的位置仅使用一次。 Good uses are as comparators for generic algorithms such as sorting, where you can then define a custom sort function where you are invoking the sort rather than further away forcing you to look elsewhere to see what you are sorting on. 很好的用途是用作排序等通用算法的比较器,然后您可以在其中定义自定义排序函数,以在其中调用排序,而不是进一步强迫您到其他地方查看正在排序的内容。

And it's not really an innovation. 这并不是真正的创新。 LISP has had lambda functions for about 30 years or more. LISP具有lambda函数已超过30年。


#4楼

Anonymous functions and expressions are useful for one-off methods that don't benefit from the extra work required to create a full method. 匿名函数和表达式对于一次性方法很有用,这些方法无法从创建完整方法所需的额外工作中受益。

Consider this example: 考虑以下示例:

 string person = people.Find(person => person.Contains("Joe"));

versus

 public string FindPerson(string nameContains, List<string> persons)
 {
     foreach (string person in persons)
         if (person.Contains(nameContains))
             return person;
     return null;
 }

These are functionally equivalent. 这些在功能上是等效的。


#5楼

It's a way of taking small operation and putting it very close to where it is used (not unlike declaring a variable close to its use point). 这是一种进行小操作并将其非常靠近使用位置的方式(与声明变量接近其使用点的方式不同)。 This is supposed to make your code more readable. 这应该使您的代码更具可读性。 By anonymizing the expression, you're also making it a lot harder for someone to break your client code if it the function is used somewhere else and modified to "enhance" it. 通过使表达式匿名化,如果该函数在其他地方使用并被修改为“增强”功能,您将使他人很难破坏您的客户端代码。

Similarly, why do you need to use foreach? 同样,为什么需要使用foreach? You can do everything in foreach with a plain for loop or just using IEnumerable directly. 您可以使用简单的for循环或直接使用IEnumerable在foreach中完成所有操作。 Answer: you don't need it but it makes your code more readable. 答:您不需要它,但是它使您的代码更具可读性。


#6楼

Lambda expression is a concise way to represent an anonymous method. Lambda表达式是表示匿名方法的一种简洁方法。 Both anonymous methods and Lambda expressions allow you define the method implementation inline, however, an anonymous method explicitly requires you to define the parameter types and the return type for a method. 匿名方法和Lambda表达式都允许您内联定义方法实现,但是,匿名方法明确要求您定义方法的参数类型和返回类型。 Lambda expression uses the type inference feature of C# 3.0 which allows the compiler to infer the type of the variable based on the context. Lambda表达式使用C#3.0的类型推断功能,该功能允许编译器根据上下文推断变量的类型。 It's is very convenient because that saves us a lot of typing! 这非常方便,因为这可以节省很多打字时间!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值