Is there the equivalent of Andalso and Orelse in C#?

Asked

Viewed 993 times

9

In VB.NET there are two interesting operators: the AndAlso and the OrElse.

When used, the logical expression is not evaluated in full immediately.

See the following example:

If (Not Usuario Is Nothing) And (Usuario.Idade = 18) Then
...
End If

Case the object Usuario be it Nothing, one NullReferenceException will be triggered even with the previous check. This problem could be circumvented like this:

If (Not Usuario Is Nothing) Then
    If (Usuario.Idade = 18) Then
        ...
    End If
End If

However, if we change the operator to AndAlso, the exception will not be triggered, because the part of the expression that is after the AndAlso will be evaluated only if the previous part is True:

If (Not Usuario Is Nothing) AndAlso (Usuario.Idade = 18) Then
...
End If

My question is: There are operators similar to these in C#?

  • 1

    In fact, I think that in C# this already happens by default, and if you want to validate the 2 anyway, you should use a symbol instead of 2. if (!usuario == null) & (usuario.idade == 18){

2 answers

9


C# comes from the line of C that prefers symbols rather than words to express much of its syntax, so:

AndAlso  =>  &&
OrElse   =>  ||

I think a lot programmer uses the wrong operator, although most of the time gives the same result, it is different to use:

And  =>  &
Or   =>  |

The former are logical working only with true and false and possess short-circuit, the latter are boolean arithmetic operators operating on all value bits.

See more about the differences.

Then in C#:

if (Usuario != null && Usuario.Idade == 18) {
    ...
}

I put in the Github for future reference.

But depending on what you want, in C# you can use other patterns that avoid checking null, even in C# 8 will have the possibility to ensure that the object is not null.

  • When I programmed with VB, all examples in books and online used operators And and Or. I think that’s why its use is more common than AndAlso and OrElse.

  • Yeah, leave yours, it’s useful too, no matter who says the same thing, collaborate to show that’s right.

  • I really didn’t know, thank you so much for the excellent explanation! :)

6

The behaviour of the logical operator to "and "logical in the C# (&&) is the same as the operator AndAlso you mentioned. Iofc for the operator "or "logical (||) and OrElse.

Operators And and Or Visual Basic is equivalent to operators & and | of C#, respectively.

I think we can all agree that this is not at all obvious.

Following Maniero’s answer, I’ll try to explain the difference here.

Operators AndAlso/&& and OrElse/|| function only with boolean expressions and are short-circuited.

Operators And/& and Or/| function as bit filters and can be used with expressions of several different types - this is a little more complex. But the important thing here is that they are not short-circuited.

Bit filtering occurs as follows: you align two values of the same type in binary and compare the bits in pairs, always comparing the n-th bit of a value with the n-th bit of another value. The result of the transaction is a third binary value of the same size, completed according to the following rules:

  • If the operation is "and", each result position will contain 1 only if both inputs are 1 at that position, and 0 otherwise;
  • If the operation is "or", each result position will contain 0 only if both inputs have 0 at that position, and 1 otherwise.

To check, do a program that performs the following operations and display the results in a console or message box:

1 & 3:

1: 0000 0000 0000 0000 0000 0000 0000 0001
3: 0000 0000 0000 0000 0000 0000 0000 0011

resultado: 0000 0000 0000 0000 0000 0000 0000 0001 (1)

1 | 3:

1: 0000 0000 0000 0000 0000 0000 0000 0001
3: 0000 0000 0000 0000 0000 0000 0000 0011
resultado: 0000 0000 0000 0000 0000 0000 0000 0011 (3)

3 & 5:

3: 0000 0000 0000 0000 0000 0000 0000 0011
5: 0000 0000 0000 0000 0000 0000 0000 0101
resultado: 0000 0000 0000 0000 0000 0000 0000 0001 (1)

3 | 5:

3: 0000 0000 0000 0000 0000 0000 0000 0011
5: 0000 0000 0000 0000 0000 0000 0000 0101
resultado: 0000 0000 0000 0000 0000 0000 0000 0111 (7)

Now the coolest: booleans in . NET are represented by one byte. The two possible values are:

true: 0000 0001
false: 0000 0000

So: true & false results in false, while true | false results in true (independent of the order of the variables).

As for the short circuit... This is a feature of most languages derived from C. Evaluation of logical expressions has two halting conditions. It stops immediately when it finds a false value for operations of the type "and" (AndAlso/&&) or true for expressions like "or" (OrElse/||). This characteristic is not used for operators And/& and Or/|.

If you use multiple logical evaluations in a single expression, the program will assemble an expression tree - I leave it to you to search how programs do this. The important thing is that each operator turns into a knot with two children, and each child is an expression. The program then resolves the tree by replacing each operator with the result of its evaluation with its child nodes. If the operators are short-circuitable, the tree may have paths removed prematurely, and these paths will never be evaluated. But if the tree contains only non-short-circuitable operators, it will necessarily evaluate all paths it has.

  • Excellent explanation, thank you very much

  • well full text, much better than much website around. show!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.