就是靠&& .NET 中的短路安全吗?
假设 myObj 为空。写这个安全吗?
if(myObj != null && myObj.SomeString != null)
我知道有些语言不会执行第二个表达式,因为 &&在执行第二部分之前评估为 false。
Assume myObj is null. Is it safe to write this?
if(myObj != null && myObj.SomeString != null)
I know some languages won't execute the second expression because the && evaluates to false before the second part is executed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的。在 C# 中,
&&
和||
是短路的,因此仅当左侧尚未确定结果时才计算右侧。另一方面,运算符&
和|
不会短路,并且始终对两边求值。规范说:
(C# 语言规范版本 4.0 - 7.12 条件逻辑运算符)
&&
和||
的一个有趣属性是它们是短路的即使它们不对布尔值进行操作,而是用户将运算符&
或|
与true
和一起重载的类型>false
运算符。(C# 语言规范版本 4.0 - 7.12.2 用户定义的条件逻辑运算符)
Yes. In C#
&&
and||
are short-circuiting and thus evaluates the right side only if the left side doesn't already determine the result. The operators&
and|
on the other hand don't short-circuit and always evaluate both sides.The spec says:
(C# Language Specification Version 4.0 - 7.12 Conditional logical operators)
One interesting property of
&&
and||
is that they are short circuiting even if they don't operate on bools, but types where the user overloaded the operators&
or|
together with thetrue
andfalse
operator.(C# Language Specification Version 4.0 - 7.12.2 User-defined conditional logical operators)
是的,C# 使用逻辑短路。
请注意,尽管 C#(以及其他一些 .NET 语言)的行为方式如此,但它是该语言的属性,而不是 CLR 的属性。
Yes, C# uses logical short-circuiting.
Note that although C# (and some other .NET languages) behave this way, it is a property of the language, not the CLR.
我知道我迟到了,但在 C# 6.0 中你也可以这样做:
这与上面的事情是一样的。
另请参阅:
问号和点是什么意思操作员 ?。在 C# 6.0 中意味着什么?
I know I'm late to the party, but in C# 6.0 you can do this too:
Which is the same thing as above.
Also see:
What does question mark and dot operator ?. mean in C# 6.0?
您的代码是安全的 - &&和||均短路。您可以使用非短路运算符 &或 |,它评估两端,但我在很多生产代码中确实没有看到这一点。
Your code is safe - && and || are both short-circuited. You can use non-short-circuited operators & or |, which evaluate both ends, but I really don't see that in much production code.
当然,它在 C# 上是安全的,如果第一个操作数为 false,则永远不会计算第二个操作数。
sure, it's safe on C#, if the first operand is false then the second is never evaluated.
这是绝对安全的。 C# 就是其中一种语言。
It is perfectly safe. C# is one of those languages.
在 C# 中,
&&
和||
是短路的,这意味着如果确定了答案,则会评估第一个条件,而忽略其余条件。在VB.NET中,
AndAlso
和OrElse
也是短路的。在javaScript中,
&&
和||
也是短路的。我提到 VB.NET 是为了表明 .net 的丑陋红发继子有时也有很酷的东西。
我提到 javaScript,因为如果您正在进行 Web 开发,那么您可能也会使用 javaScript。
In C#,
&&
and||
are short-circuited, meaning that the first condition is evaluated and the rest is ignored if the answer is determined.In VB.NET,
AndAlso
andOrElse
are also short-circuited.In javaScript,
&&
and||
are short-circuited too.I mention VB.NET to show that the ugly red-headed step-child of .net also has cool stuff too, sometimes.
I mention javaScript, because if you are doing web development then you probably might also use javaScript.
是的,C# 和大多数语言从左到右计算 if 语句。
顺便说一句,VB6 将计算整个事情,如果它为空,则抛出异常......
Yes, C# and most languages compute the if sentences from left to right.
VB6 by the way will compute the whole thing, and throw an exception if it's null...
一个例子是,
如果双方都执行,此行将导致空异常。
有趣的旁注。上面的示例比 IsNullorEmpty 方法快很多。
an example is
This line would cause a null exception if both sides executed.
Interesting side note. The above example is quite a bit faster than the IsNullorEmpty method.
虽然 C#
&&
短路是规范的一部分,但类似的 C# 运算符and
和其他模式匹配运算符不指定计算顺序,并且编译器是自由的按照它想要的任何顺序执行它们。请参阅While C#
&&
short circuiting is part of the spec, the similar C# operatorand
and other pattern matching operators do not specify an evaluation order, and the compiler is free to do them in whatever order it wants. See the note on this page