I have a method in C# called SendEvent
that returns a bool
which represents if it was successful or not. I want to loop through a number of objects and call SendEvent
on them all, and in the end, have a result variable that is a bool
, that is true
if all SendEvent
calls succeeded, and false
if at least one failed.
At first I did this:
bool result = true;
for (int i = 0; i < myObjects.Length; i++)
{
result = result && myObjects[i].SendEvent();
}
But that will cause that the SendEvent
will not be called on subsequent objects if one fails, as the right hand side of the &&
operator won't be executed if result is false
.
So I turned it around, to:
bool result = true;
for (int i = 0; i < myObjects.Length; i++)
{
result = myObjects[i].SendEvent() && result;
}
But I found that somewhat ugly. Can I use the bitwise &=
operator to always execute the SendEvent
call, and set the value of result, like this?
bool result = true;
for (int i = 0; i < myObjects.Length; i++)
{
result &= myObjects[i].SendEvent();
}
How does the &=
work on boolean values? Will it execute both sides of the operator? What will end up in the result variable?
As you can read here, both &
and &&
are defined for bool
s as "logical and", but &&
will short-circuit: in case the first operand is false
the expression on the right will not be evaluated. Regardless what the outcome of the expression on the right is, the result of the &&
expression will remain false
. This is usually a "performance hack" in the first place, but if the right expression has side-effects, could throw an exception, etc., it is something you have to take into account. The same happens for the ||
operator if the first operand is true
.
So if you want to evaluate both sides first, you can indeed use:
result = result & myObjects[i].SendEvent();
or shorter:
result &= myObjects[i].SendEvent();
As is written in the language specifications:
The operation
x && y
corresponds to the operation
x & y
except that if
x
isfalse
,y
is not evaluated, because the result of the AND operation isfalse
no matter what the value ofy
is. This is known as "short-circuit" evaluation.
Note that there is no &&=
operator (at least at the time I am writing this). This looks reasonable since usually with an ..=
operator, you would expect that the operand is first evaluated and then some operation is done on the variable to the left. Of course it is all a matter of style and taste, but I would reason that a hypothetical &&=
does not give "enough hints" that the right operand will not be called in all cases.