Compiler error CS0029: "if (int & int)"

Adapt the old code to C + + The compiler does not pass an expression like "if (a and b)"

int a = 32;
int b = 1;
if (a & b) {} <--- Compiler Error

      

Implicit conversion from Int to Bool. I can change to if ((a & b)! = 0) {}

But it is worse to read and find in many places.

Is there a way to get around this?

+3


source to share


2 answers


There is no implicit conversion in C #. And if

it only takes a boolean value.



You need to do: if ((a & b) != 0) {}

+5


source


In C #, a int

nonzero a is not automatically evaluated with true

. int

- int

, not bool

, therefore if (i)

only works if it i

is bool

.



+3


source







All Articles