就是靠&& .NET 中的短路安全吗?

发布于 2024-10-14 18:58:52 字数 165 浏览 3 评论 0原文

假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

初相遇 2024-10-21 18:58:52

是的。在 C# 中,&&|| 是短路的,因此仅当左侧尚未确定结果时才计算右侧。另一方面,运算符 &| 不会短路,并且始终对两边求值。

规范说:

&&|| 运算符称为条件逻辑运算符。它们也称为“短路”逻辑运算符。
...
运算x && y 对应于操作x & y,但仅当 xtrue 时才计算 y
...
运算x && y 计算为 (bool)x ? (bool)y:假。换句话说,x 首先被计算并转换为类型 bool。然后,如果 xtrue,则对 y 进行求值并转换为 bool 类型,这将成为以下结果操作。否则,操作结果为false

(C# 语言规范版本 4.0 - 7.12 条件逻辑运算符)

&&|| 的一个有趣属性是它们是短路的即使它们不对布尔值进行操作,而是用户将运算符 &|true一起重载的类型>false 运算符。

运算x && y 的计算结果为 T.false((T)x) ? (T)x : T.&((T)x, y),其中
T.false((T)x) 是对 T 中声明的 operator false 的调用,以及 T.&( (T)x, y) 是对所选运算符 & 的调用。另外,值(T)x只能被评估一次。

换句话说,x 首先被求值并转换为类型 T,然后对结果调用 operator false 以确定是否>x 绝对是 false
然后,如果x肯定是false,则操作的结果是先前为x计算的值转换为类型T.
否则,将计算y,并对先前为转换为类型T<的x计算的值调用所选运算符& /code> 和为 y 计算的值以产生运算结果。


(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:

The && and || operators are called the conditional logical operators. They are also called the “shortcircuiting” logical operators.
...
The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true
...
The operation x && y is evaluated as (bool)x ? (bool)y : false. In other words, x is first evaluated and converted to type bool. Then, if x is true, y is evaluated and converted to type bool, and this becomes the result of the operation. Otherwise, the result of the operation is false.

(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 the true and false operator.

The operation x && y is evaluated as T.false((T)x) ? (T)x : T.&((T)x, y), where
T.false((T)x) is an invocation of the operator false declared in T, and T.&((T)x, y) is an invocation of the selected operator &. In addition, the value (T)x shall only be evaluated once.

In other words, x is first evaluated and converted to type T and operator false is invoked on the result to determine if x is definitely false.
Then, if x is definitely false, the result of the operation is the value previously computed for x converted to type T.
Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x converted to type T and the value computed for y to produce the result of the operation.

(C# Language Specification Version 4.0 - 7.12.2 User-defined conditional logical operators)

辞旧 2024-10-21 18:58:52

是的,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.

jJeQQOZ5 2024-10-21 18:58:52

我知道我迟到了,但在 C# 6.0 中你也可以这样做:

if(myObj?.SomeString != null)

这与上面的事情是一样的。

另请参阅:
问号和点是什么意思操作员 ?。在 C# 6.0 中意味着什么?

I know I'm late to the party, but in C# 6.0 you can do this too:

if(myObj?.SomeString != null)

Which is the same thing as above.

Also see:
What does question mark and dot operator ?. mean in C# 6.0?

别忘他 2024-10-21 18:58:52

您的代码是安全的 - &&和||均短路。您可以使用非短路运算符 &或 |,它评估两端,但我在很多生产代码中确实没有看到这一点。

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.

蘑菇王子 2024-10-21 18:58:52

当然,它在 C# 上是安全的,如果第一个操作数为 false,则永远不会计算第二个操作数。

sure, it's safe on C#, if the first operand is false then the second is never evaluated.

请帮我爱他 2024-10-21 18:58:52

这是绝对安全的。 C# 就是其中一种语言。

It is perfectly safe. C# is one of those languages.

友谊不毕业 2024-10-21 18:58:52

在 C# 中,&&|| 是短路的,这意味着如果确定了答案,则会评估第一个条件,而忽略其余条件。

在VB.NET中,AndAlsoOrElse也是短路的。

在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 and OrElse 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.

那片花海 2024-10-21 18:58:52

是的,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...

孤蝉 2024-10-21 18:58:52

一个例子是,

if(strString != null && strString.Length > 0)

如果双方都执行,此行将导致空异常。

有趣的旁注。上面的示例比 IsNullorEmpty 方法快很多。

an example is

if(strString != null && strString.Length > 0)

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.

半仙 2024-10-21 18:58:52

虽然 C# && 短路是规范的一部分,但类似的 C# 运算符 and 和其他模式匹配运算符不指定计算顺序,并且编译器是自由的按照它想要的任何顺序执行它们。请参阅

While C# && short circuiting is part of the spec, the similar C# operator and 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文