System.Enum組合考慮下面的枚舉:與旗
[System.Flags]
public enum EnumType: int
{
None = 0,
Black = 2,
White = 4,
Both = Black | White,
Either = ???, // How would you do this?
}
目前,我已經寫了一個擴展方法:
public static bool IsEither (this EnumType type)
{
return
(
((type & EnumType.Major) == EnumType.Major)
|| ((type & EnumType.Minor) == EnumType.Minor)
);
}
有沒有更優雅的方式來實現這一目標?
更新:從答案中可以看出,EnumType.EenType本身沒有任何地方。
不應'Both'是'黑色| White'?你實現它的方式'Both'是'0'。 –
謝謝。更正它。 –
另外,在這種情況下,'Both'和'Either'似乎都會縮減,它們被實現爲位標誌。 –