How to use Enum with additional options (All, None)

前端 未结 3 1555
栀梦
栀梦 2021-01-05 12:38

I have an enum, which:

  • is included in my class as a property
  • it represents some values from a database table (a couple of types)
  • it is displa
3条回答
  •  臣服心动
    2021-01-05 12:41

    Codesleuth comment on another answer made me read the question again and here is an update.

    Consider the use of a flags enumeration if you are going to have multiple combination's. In your case it would mean that selecting any combination of types is a valid input.

    [Flags]
    enum MyTypes
    {
        None = 0,
        One = 1,
        Two = 2,
        Three = 4,
        Four = 8,
        All = One | Two | Three | Four
    }
    

    If the user can only select one type or all the types then use a normal enumeration:

    enum MyType
    {
        None,
        One,
        Two,
        Three,
        Four,
        All
    }
    

提交回复
热议问题