为什么我们更喜欢?到 ?? C# 中的运算符?
我最近发现我们可以使用??运算符检查空值。请检查下面的代码示例:
var res = data ?? new data();
完全相同
var res = (data==null) ? new data() : data ;
这与我检查我的整个项目源存储库和其他一些开源项目 。并且这个 ??
运算符从未被使用过。
我只是想知道这背后是否有任何原因,例如性能问题或其他什么?
编辑:
我刚刚根据 recursive & 的评论更新了我的示例代码。安东.这是一个粗心的错误。 :(
I recently found that we can use ?? operator to check nulls. Please check the below code samples:
var res = data ?? new data();
This is exactly similar to
var res = (data==null) ? new data() : data ;
I checked my whole project source repository and some of other open source projects. And this ??
operator never been used.
I just wondering is there any reason behind this, like performance problems or something?
EDIT:
I just updated my sample code based on the comments from recursive & Anton. Its a mistake in careless. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
空合并运算符在检查空值时更加清晰,这是它的主要目的。它也可以被链接起来。
虽然该运算符仅限于空检查,但三元运算符则不然。例如,
我认为人们只是不知道空合并运算符,因此他们使用三元运算符。在大多数 C 风格语言中,三进制早于 C# 存在,因此如果您对 C# 不太了解和/或使用其他语言进行编程,那么三进制是一个自然的选择。如果您要检查 null,请使用 null 合并运算符,它是为此而设计的,并且 IL 稍微优化了(将 ?? 与 if then else 进行比较)。
下面是一个比较各个使用方法的例子
,首先看一下null合并的语法,就清楚多了。三元确实很混乱。现在让我们看看 IL
Null Coalesce Only
Ternary Only
If then Else Only
IL 不是我的强项之一,所以也许有人可以编辑我的答案并对其进行扩展。我打算解释我的理论,但我不想让自己和其他人感到困惑。这三个运算符的 LOC 数量相似,但并非所有 IL 运算符执行的时间长度都相同。
The null coalesce operator is much clearer when checking for null, that is its main purpose. It can also be chained.
While that operator is limited to null checking, the ternary operator is not. For example
I think people just aren't aware of the null coalesce operator so they use the ternary operator instead. Ternary existed before C# in most C style languages so if you don't know C# inside and out and/or you programmed in another language, ternary is a natural choice. If you are checking for null though, use the null coalesce operator, it is designed for that, and the IL is slightly optimized (compare ?? to an if then else).
Here is an example comparing the use of each
First, just look at the syntax for null coalesce, it is way clearer. Ternary is really confusing. Now lets look at the IL
Null Coalesce Only
Ternary Only
If Then Else Only
IL isn't one of my strong points, so maybe someone can edit my answer and expand on it. I was going to explain my theory, but I'd rather not confuse myself and others. The number of LOC is similar for all three, but not all IL operators take the same length of time to execute.
这 ??运算符(也称为 空合并运算符)的知名度低于三元运算符,它在 .NET 2.0 和 Nullable 类型中首次亮相。不使用它的原因可能包括不知道它的存在,或者更熟悉三元运算符。
也就是说,检查 null 并不是三元运算符的唯一优点,因此它本身并不是它的替代品,更像是针对非常特定需求的更好替代方案。 :)
The ?? operator (also known as the null-coalescing operator) is less known than the ternary operator, as it made its debut with .NET 2.0 and Nullable Types. Reasons for not using it probably include not begin aware that it exists, or being more familiar with the ternary operator.
That said, checking for null is not the only thing the ternary operator is good for, so it's not a replacement for it as such, more like a better alternative for a very specific need. :)
我能想到的一个原因是该运算符是在 .NET 2.0 中引入的,因此 .NET 1.1 的代码不能拥有它。
我同意你的观点,我们应该更频繁地使用它。
参考链接
One reason I can think of is that this operator was introduced in .NET 2.0 so the code for .NET 1.1 cannot have it.
I agree with you, we should be using this more often.
ref link
基于 Bob 的回答
...是来自发布版本的 IL ...
Based on Bob's answer
... this is the IL from release builds ...
原因之一(正如其他人已经提到的)可能是缺乏意识。它也可能(就像我自己的情况一样)希望尽可能减少代码库中执行类似操作的方法的数量。因此,我倾向于将三元运算符用于所有紧凑的 if-a-condition-is-met-do-this-otherwise-do-that 情况。
例如,我发现以下两个陈述在概念层面上非常相似:
One reason (as others have already touched) is likely to be lack of awareness. It could also be (as in my own case), a wish to keep the number of approaches to do similar things in a code base down as much as possible. So I tend to use the ternary operator for all compact if-a-condition-is-met-do-this-otherwise-do-that situations.
For instance, I find the following two statements rather similar on a conceptual level:
我认为这只是其他语言的习惯。 AFAIK, ??运算符未在任何其他语言中使用。
I think it's just a habit from other languages. AFAIK, ?? operator is not used in any other language.
我本以为
相当于
I would have thought the equivalent of
would be