C# 有没有比 () => 更好的表达无参数 lambda 的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a better way to express a parameterless lambda than () =>?
提问by brendanjerwin
The ()
seems silly. is there a better way?
在()
似乎很傻。有没有更好的办法?
For example:
例如:
ExternalId.IfNotNullDo(() => ExternalId = ExternalId.Trim());
ExternalId.IfNotNullDo(() => ExternalId = ExternalId.Trim());
采纳答案by Charlie Flowers
Sort of! There is a new idiom in town, that is nice and may help you in some cases. It is not fully what you want, but sometimes I think you will like it.
有点!镇上有一个新的成语,它很好,在某些情况下可能对您有所帮助。它并不完全是您想要的,但有时我认为您会喜欢它。
Since underscore ("_") is a valid C# identifier, it is becoming a common idiom to use it as a parameter name to a lambda in cases where you plan to ignore the parameter anyway. If other coders are aware of the idiom, they will know immediately that the parameter is irrelevant.
由于下划线 ("_") 是有效的 C# 标识符,因此在您计划忽略参数的情况下,将其用作 lambda 的参数名称正成为一种常见的习惯用法。如果其他编码人员知道这个习语,他们会立即知道该参数无关紧要。
For example:
例如:
ExternalId.IfNotNullDo( _ => ExternalId=ExternalId.Trim());
Easy to type, conveys your intent, and easier on the eyes as well.
易于打字,传达您的意图,也更容易在眼睛上。
Of course, if you're passing your lambda to something that expects an expression tree, this may not work, because now you're passing a one-parameter lambda instead of a no-parameter lambda.
当然,如果您将 lambda 传递给需要表达式树的对象,这可能不起作用,因为现在您传递的是单参数 lambda 而不是无参数 lambda。
But for many cases, it is a nice solution.
但在很多情况下,这是一个不错的解决方案。
回答by Jon Skeet
No, there isn't. Lambda expressions are optimised (in terms of syntax) for the single parameter case.
不,没有。Lambda 表达式针对单参数情况进行了优化(在语法方面)。
I know that the C# team feels your pain, and have tried to find an alternative. Whether there ever will be one or not is a different matter.
我知道 C# 团队感受到了您的痛苦,并已尝试寻找替代方案。是否会有一个是另一回事。
回答by Marc Gravell
For a lambda, no: you need () =>
对于 lambda,不:您需要 () =>
Is it for a delegate or an expression? For delegates, another option is delegate {...}
. This may or may not be desirable, depending on the scenario. It is more keys, certainly...
是用于委托还是表达式?对于代表,另一种选择是delegate {...}
. 这可能是也可能不是理想的,这取决于场景。这是更多的钥匙,当然......
In some cases (not this one) you can use a target method directly - i.e.
在某些情况下(不是这个),您可以直接使用目标方法 - 即
ExternalId.IfNotNullDo(SomeMethod);
回答by Ruben Bartelink
Essentially what you're looking for is the inverse of the ??
null coalescing operator (which is called Nullable<T>.GetValueOrDefault()
under the covers) - problem is C# doesn't provide a neat OOTB answer.
本质上,您要寻找的是??
空合并运算符(在幕后调用Nullable<T>.GetValueOrDefault()
)的反函数- 问题是 C# 没有提供简洁的 OOTB 答案。
Don't know exactly what you're doing but as you are doing:
不知道你在做什么,但当你在做的时候:
ExternalId.IfNotNullDo(()=>ExternalId=ExternalId.Trim());
you might also find a use for:
您可能还会发现以下用途:
class NullExtensions
{
public T DefaultOr<T>( this T that, Func<T> transform)
{
return that!=default(T) ? transform(that) : default(T);
}
}
which would enable:
这将启用:
var result = input.DefaultOr( _ => _.Trim());
(in general, I'd be trying to steer away from reusing / mutating variables as you seem to be doing and instead going in an Introduce Explaining Variable / Split Temporary Variable direction i.e., use a new variable rather than value = value.DefaultOr( _ => _.Trim());
)
(一般来说,我会尽量避免重用/变异变量,因为您似乎正在这样做,而是朝着引入解释变量/拆分临时变量的方向前进,即使用新变量而不是value = value.DefaultOr( _ => _.Trim());
)