What does the [Flags] Enum Attribute mean in C#?

前端 未结 13 2232
慢半拍i
慢半拍i 2020-11-21 04:21

From time to time I see an enum like the following:

[Flags]
public enum Options 
{
    None    = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Op         


        
相关标签:
13条回答
  • 2020-11-21 05:16

    In extension to the accepted answer, in C#7 the enum flags can be written using binary literals:

    [Flags]
    public enum MyColors
    {
        None   = 0b0000,
        Yellow = 0b0001,
        Green  = 0b0010,
        Red    = 0b0100,
        Blue   = 0b1000
    }
    

    I think this representation makes it clear how the flags work under the covers.

    0 讨论(0)
提交回复
热议问题