UPDATE: A commenter points out that today is the 200th anniversary of the birth of George Boole; I had no idea when I scheduled this article that it would be so apropos. Happy birthday George Boole!
Here’s a little-known and seldom-used fact about C# operators: you can apply the &
and |
operators to bool
s, not just to integers. The &
and |
operators on bool
s differ from &&
and ||
in only one way: both operators always “eagerly” evaluate both operands. This is in marked contrast to the “lazily” computed evaluation of the &&
and ||
operators, which only evaluate their right hand argument if needed. Why on earth would you ever want to evaluate the right hand side if you didn’t need to? Why have this operation at all on bool
s?
A few reasons come to mind. First, sometimes you want to do two operations, and know whether both of them succeeded: Continue reading →