在VB.NET中的以下If语句中,条件将以什么顺序进行评估?
案例1:
If ( condition1 AND condition2 AND condition3 )
.
.
End If
案例2:
If ( condition1 OR condition2 OR condition3 )
.
.
End If
第三个案例:
If ( condition1 OR condition2 AND condition3 OR condition4)
.
.
End If
在VB.NET中的以下If语句中,条件将以什么顺序进行评估?
案例1:
If ( condition1 AND condition2 AND condition3 )
.
.
End If
案例2:
If ( condition1 OR condition2 OR condition3 )
.
.
End If
第三个案例:
If ( condition1 OR condition2 AND condition3 OR condition4)
.
.
End If
VB.NET从C程序员的角度来看是一个非常奇怪的东西。正如Gerrie在另一个回答中提到的,所有三个条件都会在完全评估其完整性时进行评估,没有短路。如果这是你想要的,AndAlso和OrElse可以让你挽回局面。
至于最后一个if,评估顺序如下:
If ((condition1 OR (condition2 AND condition3)) OR condition4)
一般而言:如果存在任何歧义,请使用括号明确指定计算的顺序。
VB.Net评估所有条件,因此这里的顺序并不重要。
如果要使用短路,使用AndAlso和OrElse关键字。
这并不完全回答当下的问题,因为它已经被回答了,我感到有必要对 Anton 提到的 OrElse 关键字以及它的相关关键字 AndAlso 进行扩展,因为有人会看到这个问题并想要解释这些关键字。
OrElse和AndAlso在需要显式排序评估时非常有用。
与Or和And不同的是,当使用OrElse或AndAlso时,If语句可以跳过剩余的表达式,如果第一个表达式的值符合要求,则无需进行所有表达式的计算。
在下列情况中,表达式的顺序适用于 OrElse,从左到右,但根据前一个值的结果,也不总是评估所有表达式:
if( Expression1 orelse Expression2 orelse Expression3 )
Code...
End If
If Expression1 is true, then Expression 2 and 3 are ignored. If Expression1 if False, then Expression2 is evaluated, and then Expression3 if Expression2 evaluates to True.
If (False OrElse True OrElse False ) then
Expression 1 and 2 are evaluated, Expression 3 is ignored.
End If
If (True OrElse True OrElse True ) then
only Expression 1 is evaluated.
End If
If (True OrElse True OrElse True ) then
only Expression 1 is evaluated.
End If
If (False OrElse False OrElse True ) then
All three expressions are evaluated
End If
OrElse用法的另一个示例:
If( (Object1 is Nothing) OrElse (Object2 is Nothing) OrElse (Object3 is Nothing) ) then
At least one of the 3 objects is not null, but we dont know which one.
It is possible that all three objects evaluated to null.
If the first object is null, the remaining objects are not evaluated.
if Object1 is not NULL and Object2 is null then Object3 is not evaluated
Else
None of the objects evaluated to null.
Endif
上面的例子与下面的例子相同:
If(Object1 Is Nothing)
Object 1 is Nothing
Return True
Else
If(Object2 Is Nothing)
Object 2 is Nothing
Return True
Else
If(Object3 Is Nothing)
Object 3 is Nothing
Return True
Else
One of the objects evaluate to null
Return False
End If
End If
End If
还有一个AndALso关键字:
If the first expression evaluates to false, the remaining two expressions are ignored
If( Expression1 AndAlso Expression2 AndAlso Expression3 ) Then ...
这可以这样使用:
If( (Not MyObject is Nothing) AndAlso MyObject.Enabled ) Then ...
The above will not evaluate the MyObject.Enabled if MyObject is null (Nothing)
MyObject.Enabled will ONLY be evaluated if MyObject is not Null.
If we are here, the object is NOT null, and it s Enabled property evaluated to true
Else
If we are here, it is because either the object is null, or it s enabled property evaluated to false;
End If
与"And"不同,"Or"会始终评估所有表达式:
If( (Not MyObject is Nothing) And MyObject.Enabled ) Then ...
MyObject.Enabled will ALWAYS be evaluated, even if MyObject is NULL,
--- which will cause an Exception to be thrown if MyObject is Null.
End If
但是由于使用 OrElse 和 AndAlso 可能会受到顺序的影响,因此评估一个对象是否为空应该首先完成,就像上面的示例一样。
如果 MyObject 为 NULL,则会引发异常。
Try
If( MyObject.Enabled AndAlso (Not MyObject is Nothing) ) Then ...
This means that first expression MyObject.Enabled Was evaluated to True,
Second Expression also Evaluated to True
Else
This means MyObject.Enabled evaluated to False, thus also meaning the object is not null.
Second Expression "Not MyObject is Nothing" was not evaluated.
End If
Catch(e as Exception)
An exception was caused because we attempted to evaluate MyObject.Enabled while MyObject is Nothing, before evaluating Null check against the object.
End Try
请在这里评论,如果我犯了错误或拼写错误,请注意我在两天没有睡觉后的早上5:16AM写的。