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

前端 未结 13 2635
迷失自我
迷失自我 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:35

    [DefaultValue(None)]
    public enum Orientation
    {
        None = -1,
        North = 0,
        East = 1,
        South = 2,
        West = 3
    }
    

    Then in the code you can use

    public Orientation GetDefaultOrientation()
    {
       return default(Orientation);
    } 
    

提交回复
热议问题