Differenc between "And" and "AndAlso" Operator in Asp.Net


Thursday, October 15, 2009

The 'And' operator evaluate both side, where 'AndAlso' only evaluate the right side if the left side is true.

See below examples to find difference between And and AndAlso.

Example 1:


If mystring IsNot Nothing And mystring.Contains("Rajesh") Then
' And operator evaluate both side
End If

This throws an exception if mystring = Nothing

If mystring IsNot Nothing AndAlso mystring.Contains("Rajesh") Then
' AndAlso only evaluate the right side if the left side is true
End If

This will not throws an exception.




Please see second example to make you more clear to find difference between And and AndAlso,


The "And" operator will check all conditions in the statement before continuing, whereas the Andalso operator will stop if it knows the condition is false.

Example 2:

if x = 8 And y = 9

Checks if x is equal to 8, and if y is equal to 9, then continues if both are true.

if x = 8 Andalso y = 9

Checks if x is equal to 8. If it's not, it doesn't check if y is 9, because it knows that the condition is false already.


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

No comments :