C# if-null-then-null 表达式
只是为了好奇/方便:C# 提供了我所知道的两个很酷的条件表达式功能:
string trimmed = (input == null) ? null : input.Trim();
我
string trimmed = (input ?? "").Trim();
经常遇到另一个这样的表达式:
如果输入引用为 null,则输出应该为 null。否则,输出应该是访问输入对象的方法或属性的结果。
我在第一个示例中正是这样做的,但是 (input == null) ? null :input.Trim()
非常冗长且不可读。
对于这种情况还有其他条件表达式吗,或者我可以优雅地使用 ??
运算符吗?
Just for curiosity/convenience: C# provides two cool conditional expression features I know of:
string trimmed = (input == null) ? null : input.Trim();
and
string trimmed = (input ?? "").Trim();
I miss another such expression for a situation I face very often:
If the input reference is null, then the output should be null. Otherwise, the output should be the outcome of accessing a method or property of the input object.
I have done exactly that in my first example, but (input == null) ? null : input.Trim()
is quite verbose and unreadable.
Is there another conditional expression for this case, or can I use the ??
operator elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
像 Groovy 的空安全解引用运算符之类的东西?
我猜想 C# 团队已经研究过这个问题,发现它并不像人们想象的那样简单优雅地设计......尽管我还没有听说过问题的细节。
恐怕我现在不相信该语言中有任何这样的事情......而且我还没有听说过任何相关计划,尽管这并不是说它在某个时候不会发生。
编辑:它现在将成为 C# 6 的一部分,作为“空条件运算符”。
Something like Groovy's null-safe dereferencing operator?
I gather that the C# team has looked at this and found that it's not as simple to design elegantly as one might expect... although I haven't heard about the details of the problems.
I don't believe there's any such thing in the language at the moment, I'm afraid... and I haven't heard of any plans for it, although that's not to say it won't happen at some point.
EDIT: It's now going to be part of C# 6, as the "null-conditional operator".
恐怕目前我们只能在您不想重复的情况下编写扩展方法。
Currently we can only write an extension method if you don't want to repeat yourself, I'm afraid.
您可以选择自定义
Nullify
类或NullSafe
扩展方法,如下所述:http://qualityofdata.com/2011/01/27/nullsafe-dereference-operator-in-c/用法如下如下:
You can choose between a custom
Nullify
class or aNullSafe
extension method as described here: http://qualityofdata.com/2011/01/27/nullsafe-dereference-operator-in-c/The usage will be as follows:
作为解决方法,您可以使用基于 Maybe monad< /a>.
这样使用它:
As a workaround you can use this which is based on Maybe monad.
Use it this way:
没有任何内置内容,但如果您愿意,您可以将其全部包装在扩展方法中(尽管我可能不会打扰)。
对于这个具体示例:
或更通用的版本:
There's nothing built-in, but you could wrap it all up in an extension method if you wanted (although I probably wouldn't bother).
For this specific example:
Or a more general-purpose version:
我遇到了同样的问题,我写了一些小扩展方法:
你像这样使用它;
or
or
还有可空类型的重载。
I had the same problem I wrote a few little extension methods:
You use it like this;
or
or
There are also overloads for nullable types.
我知道 Jon Skeet 已经回答了这个问题,但这个示例可能是 Elvis 运算符
?.
(又名 空条件成员访问运算符)的更清晰示例相当于...
“谢谢你,非常感谢” - 埃尔维斯·普雷斯利
I know this was already answered by Jon Skeet but this example might be a little more clear example of the Elvis operator
?.
(aka the null-conditional member access operator)is equivalent to...
"Thank You, Thank You Very Much" - Elvis Presley