C Sharp - Operators

AndAlso

VB has an AndAlso, and an OrElse operator.

1
If (anObject is null) orelse (anObject.parent)

What will happen is if the first operation pans out, then it will not compute the second operation. It’s a CPU optimization. But also in the above case if anObject is null, and you used the or statement (instead of orelse) you would get an error (something like object not found)

And and Or’s

C# has these as well

  • & the and operator
  • && is equivalent to the AndAlso operator in VB
  • | the or operator
  • || is the OrElse operator.

So

1
If ((b > 0 ) | a/b<100 ) ...

will evaluate both conditions before returning

1
If ((b > 0 ) || a/b<100 ) ... 

Will not evaluate the second operation if the first is true.

Reference: https://www.c-sharpcorner.com/article/andalso-orelse-operators-in-C-Sharp/

Null Coalescing Operator

Uses the ?? operator

For example

X ?? y 

Means – if x is non-null, evaluate to x, otherwise Y