Which is better for performance? And vs AndAlso

后端 未结 3 502
梦谈多话
梦谈多话 2020-12-30 00:50

When writing an If statement, I\'ve always used And when needed like:

If 1=1 And 2=2 Then

The only time I ever used AndA

3条回答
  •  心在旅途
    2020-12-30 01:38

    Yes, AndAlso can be faster than And, because it doesn't evaluate subsequent conditions if an earlier condition proves false.

    And is a throwback to earlier versions of Visual Basic.
    Most (I hesitate to say all) modern languages use boolean operators that short-circuit conditions that don't strictly need to be evaluated.

    e.g. && the and operator for C style languages all perform as AndAlso.

    Be careful if you've lots of code that use And and Or, a global search and replace can change existing behaviour, if the second condition involves a function call that has side effects.

    I would prefer using AndAlso and OrElse unless you specifically require the functionality provided by And & Or

提交回复
热议问题