They both are different logical and operators. && is logical and operator while & For integral types computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands, reference. One difference is that &&
will short circuit whereas &
will not.
In this particular case the logical and is more easy to understand because it is most commonly used for logical and. As mentioned earlier the && will short-circuit the condition e.g. if &&
gets false in the condition statement the further conditions on right won't be evaluated, but & wont short-circuit. See the example given below.
int a = 1;
int b = 2;
int c = 3;
bool r = a == 2 && ++b == 2;
Console.WriteLine(b);
r = a == 2 & ++b == 2;
Console.WriteLine(b);
Output
2
3
In the above example using &&
if a
is not 1 then remaining condition wont be evaluated which is not case with bitwise &
. Due to short circuiting of && the b is not incremented but with & the value of b is incremented as short-circuiting did not applied.
In my opinion using && is more appropriate here as it makes code more understandable along with short-circuiting.