If I am evaluating two variables and not two method calls does it matter weather I use ‘&&’ or ‘&’
//some logic that sets bool values boolean X = true; boolean Y = true; if (X & Y){ // perform some operation } if (X && Y){ // perform some operation }
Further a book I am using for C# 3.0 / .NET 3.5 only makes reference to the && operator, is the & operator going away?
As has been observed,
&is the bitwise AND operator. Raw binary math is seeming to be less and less common over time, with an increasing number of developers not really understanding bitwise arithmetic. Which can be a pain at times.However there are a lot of tasks that are best solved with such, in particular anything that looks at data as flags. The
&operator is 100% necessary, and isn’t going anywhere – it simply isn’t used as frequently as the boolean short-circuiting&&operator.For example: