在.NET 4中,有一个新方法Enum.HasFlag。这使您可以编写:
if ( testItem.HasFlag( FlagTest.Flag1 ) ){ // Do Stuff}IMO更具可读性。
.NET源表明这与接受的答案具有相同的逻辑:
public Boolean HasFlag(Enum flag) { if (!this.GetType().IsEquivalentTo(flag.GetType())) { throw new ArgumentException( Environment.GetResourceString( "Argument_EnumTypeDoesNotMatch", flag.GetType(), this.GetType())); } ulong uFlag = ToUInt64(flag.GetValue()); ulong uThis = ToUInt64(GetValue()); // test predicate return ((uThis & uFlag) == uFlag); }


