该编译器继续评估的前pression哪里都必须是真实的,如果第一个是假的?第一个、编译器、真实、继续

2023-09-04 01:02:30 作者:我没啥特长就是特别长

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

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

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

解决方案

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

Bebt交易所 CertiK正式发布DeepSEA 1.0编译器

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

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