如果第一个表达式为假,则编译器是否会继续计算所有表达式都必须为真的表达式?
我确信这个问题之前可能已经得到解答,所以我很抱歉,但我无法找到正确的搜索词来找到答案。
给出以下代码示例,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
否。
&&
和||
均由 短路评估。这意味着a &&如果
返回 true,并且在这两种情况下都不会计算a
为 false 并且a || b
返回 false如果a
为 true,bb
。如果由于某种原因您不想进行短路计算,您可以使用按位运算符
&
和|
。No. Both
&&
and||
are evaluated by short-circuit evaluation. This means thata && b
returns false ifa
is false anda || b
returns true ifa
is true and it will not evaluateb
in either of these cases.If for some reason you do not want short-circuit evaluation you can use the bitwise operators
&
and|
.不会。
&&
运算符会短路(这意味着在表达式的任何部分计算结果为 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.不,C# 使用短路和。所以答案是不。
如果您需要同时评估两者,请仅使用一个与号 & 来使用 NON-SHORT-CIRCUIT 运算符。
请注意单个&。
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 &.
Please note the single &.
不
No
我说的是 C# 逻辑 AND (
&&
),第二个表达式为假,因为它们都需要为真才能使表达式为真,所以编译器立即停止计算。与逻辑与相反,逻辑或 (
||
) 只需要所有表达式中只有一个表达式为真,整个表达式就为真。因此,||
运算符不会使false
求值短路,而是使编译器对 true 求值短路。现在,这是 C# 编译器的行为,但这并不意味着每个编译器都这样做,就像在 VB.NET 中一样,您有两个逻辑与运算符 (
和
,AndAlso
),以及两个逻辑或运算符(Or
、OrElse
)。And
运算符用于按位和逻辑连接,当第一个表达式返回 false 并计算另一个表达式时,它不会短路,而AndAlso
会短路当第一个逻辑表达式为假时求值。这与Or
和OrElse
相同,其中Or
不会短路,而OrElse
会短路。简而言之,我想说这取决于您正在使用的编译器。至于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.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 afalse
evaluation, the||
operator causes the compiler to short-circuit over a true evaluation.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
). TheAnd
operator, used for both bitwise and logic conjunctions, does not short-circuit when the first expression returns false and evaluate the other anyway, whileAndAlso
will short-circuit the evaluation when the first logical expression is false. That is the same withOr
andOrElse
, whereOr
doesn't short-circuit, andOrElse
does.In short, I would say that this depends on the compiler you're working with. As for C#, it does short-circuit.
它是短路的,允许您执行以下操作:
如果两个操作都被评估,则有可能引用空对象,这将是运行时异常。
It is shortcircuiting and allows you to do things like this:
if both operations were evaluated, there would be a possibility of referencing a null object, which would be a runtime exception.