该编译器继续评估的前pression哪里都必须是真实的,如果第一个是假的? [英] Does the compiler continue evaluating an expression where all must be true if the first is false?

查看:105
本文介绍了该编译器继续评估的前pression哪里都必须是真实的,如果第一个是假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这个问题可能已经被回答过了,所以我很抱歉,但我没能找到合适的搜索条件来寻找答案。

由于以下code的例子,确实 db.GetRecords()。任何()得到执行?

 字符串s =Z;
布尔X = s.IndexOfAny(新[] {A,B})> 0安培;&安培;
         。db.GetRecords()任何();
 

解决方案

没有。无论&功放;&安培; || 通过的短路计算。这意味着 A和&放大器; b 返回false如果 A 是假的, A || b 如果返回true A 是真实的,它不会评估 B 在这两种案例。

如果由于某种原因,你不想短路计算,你可以使用按位运算符&安培; |

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();

解决方案

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 |.

这篇关于该编译器继续评估的前pression哪里都必须是真实的,如果第一个是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆