如果第一个表达式为假,则编译器是否会继续计算所有表达式都必须为真的表达式?

发布于 2024-09-28 18:28:45 字数 243 浏览 5 评论 0原文

我确信这个问题之前可能已经得到解答,所以我很抱歉,但我无法找到正确的搜索词来找到答案。

给出以下代码示例,db.GetRecords().Any() 是否被执行?

string s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &&
         db.GetRecords().Any();

I'm sure this question has probably been answered before, so I apologize, but I wasn't able to find the proper search terms to find the answer.

Given the following code example, does db.GetRecords().Any() get executed?

string s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &&
         db.GetRecords().Any();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

静谧 2024-10-05 18:28:45

否。 &&|| 均由 短路评估。这意味着 a &&如果 a 为 false 并且 a || b 返回 false如果 a 为 true,b 返回 true,并且在这两种情况下都不会计算 b

如果由于某种原因您不想进行短路计算,您可以使用按位运算符 &|

No. Both && and || are evaluated by short-circuit evaluation. This means that a && b returns false if a is false and a || b returns true if a is true and it will not evaluate b in either of these cases.

If for some reason you do not want short-circuit evaluation you can use the bitwise operators & and |.

我一直都在从未离去 2024-10-05 18:28:45

不会。&& 运算符会短路(这意味着在表达式的任何部分计算结果为 false 后,它会停止计算表达式)。

|| 运算符也会短路,但在表达式的任何部分计算结果为 true 后停止计算。

No. The && operator short-circuits (which means it stops evaluating the expression after any part of the expression evaluates to false).

The || operator also short-circuits but stops evaluating after any part of the expression evaluates to true.

吃→可爱长大的 2024-10-05 18:28:45

不,C# 使用短路和。所以答案是

如果您需要同时评估两者,请仅使用一个与号 & 来使用 NON-SHORT-CIRCUIT 运算符。

tring s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &
         db.GetRecords().Any();

请注意单个&

No, C# use short circuit and. So the answer is no.

If you need to evaluate both, use NON-SHORT-CIRCUIT operator by using just one ampersand &.

tring s = "Z";
bool x = s.IndexOfAny(new[] { 'A', 'B' }) > 0 &
         db.GetRecords().Any();

Please note the single &.

叫嚣ゝ 2024-10-05 18:28:45

我说的是 C# 逻辑 AND (&&),第二个表达式为假,因为它们都需要为真才能使表达式为真,所以编译器立即停止计算。

&&运算符(C# 参考)

条件与运算符 (&&) 对其布尔操作数执行逻辑与,但仅在必要时计算其第二个操作数。

与逻辑与相反,逻辑或 (||) 只需要所有表达式中只有一个表达式为真,整个表达式就为真。因此,|| 运算符不会使 false 求值短路,而是使编译器对 true 求值短路。

||运算符(C# 参考)

现在,这是 C# 编译器的行为,但这并不意味着每个编译器都这样做,就像在 VB.NET 中一样,您有两个逻辑与运算符 ( AndAlso),以及两个逻辑或运算符(OrOrElse)。 And 运算符用于按位和逻辑连接,当第一个表达式返回 false 并计算另一个表达式时,它不会短路,而 AndAlso 会短路当第一个逻辑表达式为假时求值。这与 OrOrElse 相同,其中 Or 不会短路,而 OrElse 会短路。

和运算符 (Visual Basic)

对两个布尔表达式执行逻辑连接,或对两个数值表达式执行按位连接。

AndAlso 运算符 (Visual Basic )

对两个表达式执行短路逻辑与。

或运算符 (Visual Basic)

对两个布尔表达式执行逻辑或操作,或对两个数值表达式执行按位或操作。

OrElse 运算符 (Visual Basic)

对两个表达式执行短路包含逻辑或。

简而言之,我想说这取决于您正在使用的编译器。至于C#,它确实短路了。

I say that for a C# logic-AND (&&), the second that an expression is false, since they all need to be true for the expression to be true, the compiler stops evaluating immediately.

&& Operator (C# Reference)

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

Contrarely to a logic-AND, the logic-OR (||) only requires only one expression among all to be true, for the whole expression to be true. So, instead of short-circuiting over a false evaluation, the || operator causes the compiler to short-circuit over a true evaluation.

|| Operator (C# Reference)

Now, that is the behaviour of the C# compiler, but it doesn't mean every compiler bahves that way, as in VB.NET, you have two logic-AND operators (And, AndAlso), and two logic-OR operators (Or, OrElse). The And operator, used for both bitwise and logic conjunctions, does not short-circuit when the first expression returns false and evaluate the other anyway, while AndAlso will short-circuit the evaluation when the first logical expression is false. That is the same with Or and OrElse, where Or doesn't short-circuit, and OrElse does.

And Operator (Visual Basic)

Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions.

AndAlso Operator (Visual Basic)

Performs short-circuiting logical conjunction on two expressions.

Or Operator (Visual Basic)

Performs a logical disjunction on two Boolean expressions, or a bitwise disjunction on two numeric expressions.

OrElse Operator (Visual Basic)

Performs short-circuiting inclusive logical disjunction on two expressions.

In short, I would say that this depends on the compiler you're working with. As for C#, it does short-circuit.

始终不够 2024-10-05 18:28:45

它是短路的,允许您执行以下操作:

if(ob && ob.somefunc()) { ... }

如果两个操作都被评估,则有可能引用空对象,这将是运行时异常。

It is shortcircuiting and allows you to do things like this:

if(ob && ob.somefunc()) { ... }

if both operations were evaluated, there would be a possibility of referencing a null object, which would be a runtime exception.

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