[英]Compiler Error CS0029: “if (int & int)”
Adapt the old code in C + + The compiler does not pass the type expression "if (a & b)" 在C ++中调整旧代码编译器不传递类型表达式“if(a&b)”
int a = 32;
int b = 1;
if (a & b) {} <--- Compiler Error
Implicit conversion from Int to Bool. 从Int到Bool的隐式转换。 I can change to
if ((a & b)! = 0) {}
But it is worse to read and found in many places. 我可以改为
if ((a & b)! = 0) {}
但是在许多地方阅读和找到它会更糟。
Is there any way around this? 有没有办法解决?
There is not it implicit conversion in C#. 在C#中没有隐式转换。 And
if
only accept boolean. if
只接受布尔值。
You have to do : if ((a & b) != 0) {}
你必须这样做:
if ((a & b) != 0) {}
In C#, an int
being non-zero does not evaluate to true
automatically. 在C#中,
int
为非零不会自动计算为true
。 An int
is an int
and not a bool
, so if (i)
only works if i
is a bool
. int
是int
而不是bool
,所以if (i)
只有在i
是bool
时才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.