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

后端 未结 6 861
伪装坚强ぢ
伪装坚强ぢ 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:21

    How about something like

    var all = Enum.GetValues(typeof(MyEnum)).Cast().Last() * 2 - 1;
    

    basically

    all = max*2-1
    

    this only works if all values are present from 1 to the max value.

    1,2,4...32,64...

提交回复
热议问题