vb.net (OrElse and Or) and (AndAlso and And) - 什么时候使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8409467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(OrElse and Or) and (AndAlso and And) - When to use?
提问by aer
What is the difference between (OrElse and Or) and (AndAlso and And)? Is there any difference in their performances, let say the correctness benefit?? Is there any situation that I shoudn't use OrElse and AndAlso?
(OrElse and Or) 和 (AndAlso and And) 有什么区别?他们的表现有什么不同吗,比如说正确性好处?有什么情况我不应该使用 OrElse 和 AndAlso?
回答by
Or/And
will alwaysevaluate both1the expressions and then return a result. They are notshort-circuiting.
Or/And
将始终评估双方1个表达式,然后返回结果。它们没有短路。
OrElse/AndAlso
are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation of the left expression alone. (That means: OrElse
will only evaluate the right expression if the left expression is false, and AndAlso
will only evaluate the right expression if the left expression is true.)
OrElse/AndAlso
正在短路。仅当无法仅从左侧表达式的评估中确定结果时,才会评估右侧表达式。(这意味着:OrElse
如果左表达式为假,AndAlso
则仅对右表达式求值,如果左表达式为真,则仅对右表达式求值。)
Assuming that no side effectsoccur in the expressions and the expressions are not dependent(and any execution overhead is ignored), then they are the same.
假设表达式中没有出现副作用并且表达式不依赖(并且忽略任何执行开销),那么它们是相同的。
However, in many cases it is that the expressions aredependent. For instance, we want to do something when a List is not-Nothing and has more than one element:
然而,在许多情况下,表达式是相关的。例如,当 List 不是 Nothing 并且有多个元素时,我们想要做一些事情:
If list IsNot Nothing AndAlso list.Length > 0 Then .. 'list has stuff
This can also be used to avoid an "expensive" computation (or side-effects, ick!):
这也可以用来避免“昂贵”的计算(或副作用,哎呀!):
If Not Validate(x) OrElse Not ExpensiveValidate(x) Then .. 'not valid
Personally, I find that AndAlso
and OrElse
are the correctoperators to use in all but the 1% - or less, hopefully! - of the cases where a side-effect isdesired.
就个人而言,我发现,AndAlso
和OrElse
是正确的运营商使用,但在所有的1% -或更小,有希望!-需要副作用的情况。
Happy coding.
快乐编码。
1An Exception thrown in the first expression will prevent the second expression from being evaluated, but this should hardly be surprising ..
1在第一个表达式中抛出的异常将阻止对第二个表达式进行求值,但这应该不足为奇..
回答by Gideon Engelberth
Besides the short-circuiting mentioned in the other answers, Or
/And
are usable as bitwise operators where OrElse
/AndAlso
are not. Bitwise operations include combining values of Flags enums, such as the FileAttributesenumeration where you might indicate a file is both read only and hidden by FileAttributes.ReadOnly Or FileAttributes.Hidden
除了其他答案中提到的短路之外,Or
/And
可用作OrElse
/AndAlso
不是的按位运算符。位运算包括组合 Flags 枚举的值,例如FileAttributes枚举,您可以在其中指示文件既是只读的又被隐藏FileAttributes.ReadOnly Or FileAttributes.Hidden
回答by davidsleeps
The difference is that OrElse and AndAlso will short-circuit based on the first condition, meaning that if the first condition doesn't pass, the second (or more) conditions will not be evaluated. This is particularly useful when one of the conditions might be more intensive than the other.
不同之处在于 OrElse 和 AndAlso 将根据第一个条件短路,这意味着如果第一个条件不通过,则不会评估第二个(或更多)条件。当其中一种情况可能比另一种情况更严重时,这尤其有用。
Example where Or
is fine (both conditions evaluated):
示例 whereOr
很好(评估两个条件):
If Name = "Fred" Or Name = "Sam" Then
It really doesn't matter which way around they are evaluated
它们的评估方式真的无关紧要
The following AndAlso
is useful because the second condition might fail
以下AndAlso
是有用的,因为第二个条件可能会失败
If Not SomeObject Is Nothing AndAlso CheckObjectExistsInDatabase(SomeObject) Then
This allows for the first condition to check whether the object has been set and only if it has been set will go and check the database (or some other task). If this had been a plain And
keyword, both would be evaluated.
这允许第一个条件检查对象是否已设置,并且仅当它已设置时才会去检查数据库(或某些其他任务)。如果这是一个普通And
关键字,则两者都将被评估。
回答by dbasnett
@Gideon - glad someone pointed that out. Here is a simple test that shows the dramatic impact of AndAlso:
@Gideon - 很高兴有人指出了这一点。这是一个简单的测试,显示了 AndAlso 的巨大影响:
Dim tm As New Stopwatch
Const tries As Integer = 123456
Dim z As Integer = 0
Dim s() As String = New String() {"0", "one"}
Debug.WriteLine("AndAlso")
For x As Integer = 0 To s.Length - 1
z = 0
tm.Restart() 'restart the stopwatch
For y As Integer = 0 To tries
If s(x) = x.ToString AndAlso s(x) = y.ToString Then '<<<<<<<<<<
z += 1
End If
Next
tm.Stop()
Debug.WriteLine(x.ToString.PadRight(3, " "c) & z.ToString.PadRight(10, " "c) & tm.Elapsed.ToString)
Next
Debug.WriteLine("And")
For x As Integer = 0 To s.Length - 1
z = 0
tm.Restart() 'restart the stopwatch
For y As Integer = 0 To tries
If s(x) = x.ToString And s(x) = y.ToString Then '<<<<<<<<<<
z += 1
End If
Next
tm.Stop()
Debug.WriteLine(x.ToString.PadRight(3, " "c) & z.ToString.PadRight(10, " "c) & tm.Elapsed.ToString)
Next