Pages

Sunday, 4 November 2012

Difference between And("&") and AndAlso("&&") Operator

And(“&”) Operator:
  Ø  It evaluates both sides even first condition goes wrong.
  Ø  It takes longer to execute for long expression.
  Ø  Example:
‘It throws exception.
If dt IsNot Nothing And dt.Rows.Count > 0
        ‘Bind Gridview Logic
End If

AndAlso(“&&”) Operator:
  Ø  It only evaluates the right side if the left side is true.
  Ø  Its faster compare to And operator.
  Ø  Example:
‘Works fine without exception.
If dt IsNot Nothing AndAlso dt.Rows.Count > 0
        ‘Bind Gridview Logic.
Else
        ‘No data found.
End If

Note: Same logic can apply for Or(“|”) and OrElse(“||”)

1 comment:

  1. The difference between "and" and "andalso" lies in programming logic. Card For Gaming "And" evaluates both conditions, executing if both are true. "

    ReplyDelete