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.
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){
– Francisco
Related: What is the difference between & and &&?
– Jéf Bueno