As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




已关闭8年。




在以下两种方法中,您更喜欢阅读哪一种?
还有另一种(更好的?)方法来检查是否设置了标志吗?
 bool CheckFlag(FooFlag fooFlag)
 {
      return fooFlag == (this.Foo & fooFlag);
 }


 bool CheckFlag(FooFlag fooFlag)
 {
      return (this.Foo & fooFlag) != 0;
 }

请投票选择您喜欢的方法。

最佳答案

这两个表达式执行不同的操作(如果fooFlag设置了多个位),所以哪个更好取决于您想要的行为:

fooFlag == (this.Foo & fooFlag) // result is true iff all bits in fooFlag are set


(this.Foo & fooFlag) != 0       // result is true if any bits in fooFlag are set

09-09 18:56