何时使用或不使用 Lambda 表达式 [英] when to use or not Lambda Expressions

查看:37
本文介绍了何时使用或不使用 Lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 lambda 表达式在语言的某些方面已成为非常有用的工具.我一直在使用它们,大多数时候它们非常适合并且使代码更短,也许更清晰.

I see lambda expressions have become a very useful tool at some points in the language. I've been using them a lot and most of the time they fit really nice and make the code shorter and perhaps clearer.

现在..我已经看到了一些,我会说过度使用它们.有些人非常喜欢它们,并试图在任何可能的地方使用它们.. 有时 C# 代码看起来像一种函数式语言.

Now.. I've seen some , I would say excessive use of them. Some people like them so much that try to use them everywhere they can.. Some times the C# code looks like a functional language.

其他不利因素是使用 lambda 反射的成本以及对调试不友好.

Other factors against are the cost using reflection by lambda and that not friendly to debugging.

我想听听关于或多或少使用 lambda 表达式有多好以及代码有多清晰的意见.

I would like to hear opinions about how good and how code clear it is to use more or less the lambda expressions.

(这不是更好的例子,但假设它是触发器)

(this is not the better example, but let's say it was the trigger)

我正在编写以下代码.delegate { return null; 的使用} 帮助我避免每次必须使用它时都必须询问事件是否为空.

I was writing the following code. The use of the delegate { return null; } helps me avoid having to ask if the event is null or not every time I have to use it.

public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = delegate { return null;}

我正在使用 resharper 和明智的 resharper(即使它有时会直接吃掉内存)给了我以下建议

Im using resharper and the wise resharper( even it some times literaly eats the memory) made me the following suggestion

public delegate ContactCellInfo.Guest AddGuest();
public event AddGuest GuestRequest = () => null;

在我看来,使用委托的代码看起来更清晰.我并不反对 Lamdba 表达,只是想听听一些关于如何以及何时使用它们的建议.

At my point of view the code using the delegate looks clearer. I am not against the Lamdba expression just would like to hear some advices on how and when to use them.

推荐答案

这里有两个问题.

首先,对于您的示例,使用 lambda 与使用匿名委托语法.编译器生成的代码将是相同的,因此不会归结为性能差异,而是可读性差异.

First, as for your example, using a lambda vs. using the anonymous delegate syntax. The generated code by the compiler will be identical, so it does not come down to a performance difference, but rather a readability difference.

就我个人而言,我发现 lambda 语法易于遵循.我发现 lambda 语法几乎总是比匿名委托语法更简洁、更简洁、更易于理解,因此我几乎总是喜欢它.

Personally, I find the lambda syntax easy to follow. I find that the lambda syntax is almost always cleaner, more concise, and more understandable than the anonymous delegate syntax, so I prefer it nearly always.

至于在整个代码中使用 lambda 表达式 - 就我个人而言,我是它们的重度用户.我发现它们通常比定义很多方法更容易.如果一段代码不会被任何其他方法重用(它只会被调用并存在于一个地方),我会用一个 lambda 来表达它.

As for using lambda expressions throughout the code - Personally, I am a fairly heavy user of them. I find that they often make life much easier than having lots of methods defined. If a piece of code is not going to be reused by any other methods (it will only be called and exist in one place), I will use a lambda to express it.

如果一段代码将被多次使用,则应将其提取到(非匿名)方法中.此外,如果一段代码是可以并且应该被测试的,我倾向于为它制定一种方法,因为这可以简化可测试性.

If a piece of code is going to be used more than once, it should be pulled out into a (non-anonymous) method. Also, if a piece of code is something that could and should be tested, I tend to make a method for it, since that eases testability.

这篇关于何时使用或不使用 Lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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