为什么会失败
基本上和这个是一样的:
if (someInteger) // C or C++
对
if (someInteger != 0) // C#
基本上,C# 在逻辑运算符和条件方面要严格得多 - 它迫使您使用 bool 或可转换为 bool 的东西。
顺便说一句,这也是为什么在 C# 中这不仅仅是一个警告,而是一个全面的错误:
int x = ...;
if (x = 10) // Whoops - meant to be == but it's actually an assignment
如果您以这种方式看到比较:
if (10 == x)
这通常是开发人员试图避免出现上述错误 - 但在 C# 中不需要这样做,除非您真的与常量 bool 值进行比较。
解决问题
我怀疑你只需要:
if (((info[i].location & 0x8) != 0)) ||
((info[i].location & 0x100) != 0)) ||
((info[i].location & 0x200) != 0)))
您可能不需要 all 这些括号......但另一种选择是使用 one 测试:
if ((info[i].location & 0x308) != 0)
毕竟,您只是在测试是否设置了这三个位中的任何一个...
您还应该考虑使用基于标志的枚举:
[Flags]
public enum LocationTypes
{
Foo = 1 << 3; // The original 0x8
Bar = 1 << 8; // The original 0x100
Baz = 1 << 9; // The original 0x200
}
那么你可以使用:
LocationTypes mask = LocationTypes.Foo | LocationTypes.Bar | LocationTypes.Baz;
if ((info[i].location) & mask != 0)
或者使用Unconstrained Melody:
LocationTypes mask = LocationTypes.Foo | LocationTypes.Bar | LocationTypes.Baz;
if (info[i].location.HasAny(mask))