I know that ||
and &&
are defined as short-circuit operators in C#, and such behaviour is guaranteed by the language specification, but do
No, the &=
and |=
operators are not doing short-circuit evaluation.
They are pseudo-operators, which the compiler converts into use of the &
and |
operators.
This code:
allEven &= IsEven(numbers[i]);
is exactly equivalent to:
allEven = allEven & IsEven(numbers[i]);
If you want the short circuited check, then you have to write it out using the short circuiting versions of the operators:
allEven = allEven && IsEven(numbers[i]);
There is no &&=
pseudo operator, but the code above is exactly what the compiler would do if there was one.