Home > Back-end >  Why does the AND statement in VB compile to a & operator?
Why does the AND statement in VB compile to a & operator?

Time:01-19

I am trying to convert VB.NET code to C#. I have the following:

If IsDataProperty(p) And (p.Name.StartsWith("ref_") = False) Then
 ...

If I use a decompiler to see what the C# version looks like, I get this:

if (this.IsDataProperty(p) & !p.Name.StartsWith("ref_")) {
...

The AND operator in VB compiled to & C# operator.
Shouldn't the code be with && operator:

 if (this.IsDataProperty(p) && !p.Name.StartsWith("ref_")) {
...

Logically speaking, in the VB code, if IsDataProperty(p) is false, the entire statement will be false.

CodePudding user response:

VB.NET has special keywords for short circuiting.

bool valueAnd = conditionA && conditionB;
bool valueOr = conditionA || conditionB;
Dim valueAnd As Boolean = conditionA AndAlso conditionB
Dim valueOr As Boolean = conditionA OrElse conditionB

CodePudding user response:

The equivalent of And in VB.NET really is &. To get C#'s && you should have used "AndAlso" in VB.NET.

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/andalso-operator

  • Related