简体   繁体   English

编译器错误CS0029:“if(int&int)”

[英]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 . intint而不是bool ,所以if (i)只有在ibool时才有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string”错误 - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' error 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string” - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39; - CS0029: Cannot implicitly convert type 'int' to 'bool' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39;-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate' 错误:CS0029 无法隐式转换类型 'System.Collections.Generic.List<int> '到'X'?</int> - Error : CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'X'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM