Neatest way to 'OR' all values in a Flagged Enum?

后端 未结 6 900
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 09:43

Given the enum:

[Flags]
public enum mytest
{
    a = 1,
    b = 2,
    c = 4
}

I\'ve come up with two ways to represent all va

6条回答
  •  死守一世寂寞
    2021-01-17 10:04

    The easiest way to ensure that all of the enum's bits are set it to just set all bits:

    mytest allValues = (mytest)int.MaxValue;
    

    This assumes that there's no problem setting bits that don't correspond to any enum, but that's likely true. You can AND this with any enum value and it will come out true, which is most likely the end goal.

提交回复
热议问题