Is there an equivalent to AndAlso and OrElse in C #?

8

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

When used, the logical expression is not fully evaluated at once.

See the following example:

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

If the object Usuario is Nothing , a NullReferenceException will be triggered, even with the previous check. This problem could be bypassed 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 AndAlso will only be evaluated if the previous part is True :

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

My question is: Are there operators similar to these in C #?

    
asked by anonymous 27.07.2017 / 13:52

2 answers

8

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

AndAlso  =>  &&
OrElse   =>  ||

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

And  =>  &
Or   =>  |

The former are logical only working with true and false and have short-circuit , the latter are arithmetic operators booleans operating on all bits of values.

See more about the differences .

Then in C #:

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

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

    
27.07.2017 / 13:57
5

Logical operator behavior for and in C # (% with%) is the same as the && operator you mentioned. Same for the or logical operator ( AndAlso ) and || .

The OrElse and And operators of Visual Basic are equivalent to the Or and & operators, respectively.

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

Following the bigown answer, I'll try to explain the difference here.

The operators | / AndAlso and && / OrElse work only with boolean expressions and are short-circuited.

The || / And and & / Or operators operate as bit filters and can be used with expressions of several different types - this is a bit 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 nth bit of a value with the nth bit of another value. The result of the operation is a third binary value of the same size, filled in according to the following rules:

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

To verify, make a program that performs the following operations and displays 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 a byte. The two possible values are:

true: 0000 0001
false: 0000 0000

Then, | results in true & false , while false results in true | false (regardless of the order of variables).

Short circuit ... This is a characteristic of most languages derived from C. The evaluation of logical expressions has two stopping conditions. It stops immediately when it encounters a false value for operations of type "and" ( true / AndAlso ) or true for expressions of type "or" ( && /% with%). This feature is not used for operators OrElse / || and And / & .

If you use several logical evaluations in a single expression, the program will assemble an expression tree - I leave it to your discretion to search how the programs do this. The important thing is that each operator becomes a node with two children, and each child is an expression. The program then resolves the tree by substituting each operator for the result of its evaluation with its child nodes. If the operators are short-circuited, the tree may have paths removed prematurely, and those paths will never be evaluated. But if the tree contains only non-short-circuiting operators, it will necessarily evaluate every path you have.

    
27.07.2017 / 13:58