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

前端 未结 13 2231
慢半拍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:00

    @Nidonocu

    To add another flag to an existing set of values, use the OR assignment operator.

    Mode = Mode.Read;
    //Add Mode.Write
    Mode |= Mode.Write;
    Assert.True(((Mode & Mode.Write) == Mode.Write)
      && ((Mode & Mode.Read) == Mode.Read)));
    

提交回复
热议问题