Why is && preferable to & and || preferable to |?
I asked someone who’s been programming for years and his explanation was:
For example, in if (bool1 && bool2 && bool3) { /*DoSomething*/ }, bool1 has to be true for it to test bool2 which has to be true before moving on to bool3, etc. If I’d used a single & instead there is no order to the test even if all of them have to be true to progress to the next line, so why does it matter anyway?
Note: I’d like to point out that I’m the programming equivalent of a toddler and this is not a serious or urgent question. It’s more a matter of understanding why things should be done a certain way as opposed to another.
In most cases,
&&and||are preferred over&and|because the former are short-circuited, meaning that the evaluation is canceled as soon as the result is clear.Example:
If
CanExecutereturnsfalse, the complete expression will befalse, regardless of the return value ofCanSave. Because of this,CanSaveis not executed.This is very handy in the following circumstance:
TryGetValuereturnsfalseif the supplied key is not found in the dictionary. Because of the short-circuiting nature of&&,value.Contains("test")is only executed, whenTryGetValuereturnstrueand thusvalueis notnull. If you would use the bitwise AND operator&instead, you would get aNullReferenceExceptionif the key is not found in the dictionary, because the second part of the expression is executed in any case.A similar but simpler example of this is the following code (as mentioned by TJHeuvel):
CanExecuteis only executed ifopis notnull. Ifopisnull, the first part of the expression (op != null) evaluates tofalseand the evaluation of the rest (op.CanExecute()) is skipped.Apart from this, technically, they are different, too:
&&and||can only be used onboolwhereas&and|can be used on any integral type (bool,int,long,sbyte, …), because they are bitwise operators.&is the bitwise AND operator and|is the bitwise OR operator.To be very exact, in C#, those operators (
&,|[and^]) are called “Logical operators” (see the C# spec, chapter 7.11). There are several implementations of these operators:int,uint,longandulong, chapter 7.11.1):They are implemented to compute the bitwise result of the operands and the operator, i.e.
&is implement to compute the bitwise logicalANDetc.They are implemented to perform the logical operation of the underlying type of the enumeration.
The result is not computed using bitwise calculations. The result is basically looked up based on the values of the two operands, because the number of possibilities is so small.
Because both values are used for the lookup, this implementation isn’t short-circuiting.