Choosing the default value of an Enum type without having to change values

前端 未结 13 2575
迷失自我
迷失自我 2020-12-04 07:46

In C#, is it possible to decorate an Enum type with an attribute or do something else to specify what the default value should be, without having the change the values? The

13条回答
  •  自闭症患者
    2020-12-04 08:27

    Actually an enum's default is the first element in the enum whose value is 0.

    So for example:

    public enum Animals
    {
        Cat,
        Dog,
        Pony = 0,
    }
    //its value will actually be Cat not Pony unless you assign a non zero value to Cat.
    Animals animal;
    

提交回复
热议问题