I'm fairly new to .NET programming, and I recently stumbled across AndAlso. As I understand it, AndAlso evaluates the first expression, if it is true then it evaluates the second...and so on. And, on the other hand, always evaluates all terms in the expression.
If my understanding is correct, would AndAlso always be (marginally) more efficient. If this is the case, why use And at all?
Best How To :
And is bitwise.
AndAlso is boolean.
Correctness wise, if we disregard short-circuting,
And can always be used in all cases where
AndAlso can be used (unless you get creative), but the other way round is not true.
And is a "universal" operator that will "do the right thing" in both the bitwise and logical contexts, but is not optimized for either.
AndAlso can only be used in the boolean context, but it is optimized for it.
Visual Basic did not use to have
And was the only choice. Now that VB has
should may be used for:
- bitwise math (performing the bitwise AND over two numbers)
- expressions with side effects (you want to disable short-circuiting and have both parts of the expression always evaluated). I would certainly not call this a recommended practice, because you might end up with very unobvious code, but it is probably okay if you have legacy code upgraded from VB6.