Comparing enum flags in C#

后端 未结 7 1507
囚心锁ツ
囚心锁ツ 2021-02-07 18:37

I need to detect if a flag is set within an enum value, which type is marked with the Flag attribute.

Usually it is made like that:

(value & flag) ==         


        
7条回答
  •  逝去的感伤
    2021-02-07 19:13

    I have used this to compare flags

    public static bool IsSet(this T input, T match)
    {
        return (Convert.ToUInt32(input) & Convert.ToUInt32(match)) != 0;
    }
    

    Here you can do the different conversions. From int to short to long.

提交回复
热议问题