短路:OrElse与Or结合 [英] Shortcircuiting: OrElse combined with Or

查看:96
本文介绍了短路:OrElse与Or结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下内容...

If I have the following ...

a OrElse b 

...并且a为 True ,那么显然b永远不会被评估.但是,如果我添加Or,那又是什么?

... and a is True then clearly b is never evaluated. But if I add an Or, then what?

a OrElse b Or c

是否/应该进行评估?如果我放在括号里怎么办?

Does/should c get evaluated? And what if I put in some brackets?

很抱歉,如果这是基本步骤.当然,我可以自己测试答案,但是找不到在这里或其他地方回答的问题.与 OrElse 有关的问题很多,但对与 OrElse

Apologies if this is basic. Of course I can test for the answer myself but I can't find this question answered here or elsewhere. Lots of questions dealing with Or versus OrElse but nothing dealing with Or with OrElse

推荐答案

这是一个运算符优先级问题.相关文档在这里:
http://msdn.microsoft.com/en-us/library/fw84t893.aspx?ppud = 4

This is an operator precedence problem. The relevant documentation is here:
http://msdn.microsoft.com/en-us/library/fw84t893.aspx?ppud=4

重要摘录:

  • 具有相同优先级的运算符按照在表达式中出现的顺序从左到右进行评估.
  • Operators with equal precedence are evaluated left to right in the order in which they appear in the expression.

包含性取舍(或,OrElse )

因此,我们在这里了解到的是Or和OrElse具有相同的优先级,并且具有相同优先级的运算符从左到右进行求值.

So what we learn here is that Or and OrElse have the same precedence and that operators with the same precedence are evaluated from left to right.

因此,我希望在a为true的情况下,不会评估b.但是,c仍然会.在a为false的情况下,将评估b,并且无论b的值如何,或"运算符都将评估c.因此,是的,始终会评估c.

Therefore, I would expect that in cases where a is true, b is not evaluated. However, c still will be. In cases where a is false, b is evaluated and regardless of the b's value the Or operator will evaluate c. So, yes, c is always evaluated.

实际上,除非有充分的理由使用Or,否则通常应在代码中首选OrElse.现在存在Or主要是为了向后兼容.

As a practical matter, you should generally prefer OrElse in your code unless you have a good reason to use Or. Or exists now mainly for backwards compatibility.

这篇关于短路:OrElse与Or结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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