2

I always use AndAlso while checking multiple conditions as it doesn't evaluate right side unless left one is true. I don't see any situation where someone would like to evaluate right side even if left one fails. If it was needed then why they didn't include same in C#.

Update:

As accepted answer pointed out that it exists because it is used for bitwise operation, that fine enough but I still think they would have overloaded And operator to serve both purposes and just not created AndAlso. If anyone can pour some light on it, this question is still open :)

Imad
  • 7,126
  • 12
  • 55
  • 112
  • 4
    They included the same in c#. In c# you can use `&` (And) or `&&` (AndAlso) – Pikoh Apr 26 '17 at 09:17
  • c# does have the same. & = And, && = AndAlso. – Malcor Apr 26 '17 at 09:18
  • @Pikoh thanks, never knew that :) – Imad Apr 26 '17 at 09:19
  • see a good answer on [this question](http://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net) – OSKM Apr 26 '17 at 09:26
  • 1
    From Paul Vick, MSFT, who partook in the decision to introduce AndAlso: [The Ballad of AndAlso and OrElse](http://www.panopticoncentral.net/2003/08/18/the-ballad-of-andalso-and-orelse/). – Andrew Morton Apr 26 '17 at 13:41

2 Answers2

1

They included the same in C#. In C# you can use & (And) or && (AndAlso).

There's no real use case i can imagine for the not short-circuit operator when comparing booleans, but And can be used with numeric values, and it then does a bitwise comparison. That's why it exists. But when comparing boolean types, you'll always be using the short-circuit version.

Pikoh
  • 7,582
  • 28
  • 53
0

And is also a bit operator. Here is an example showing a mix of And an AndAlso.

    Dim foo? As Integer = 5

    If foo.HasValue AndAlso (foo And 1) = 1 AndAlso (foo And 4) = 4 Then
        Stop
    End If
dbasnett
  • 11,334
  • 2
  • 25
  • 33