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

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

    enum Orientations
    {
        None, North, East, South, West
    }
    private Orientations? _orientation { get; set; }
    
    public Orientations? Orientation
    {
        get
        {
            return _orientation ?? Orientations.None;
        }
        set
        {
            _orientation = value;
        }
    }
    

    If you set the property to null will return Orientations.None on get. The property _orientation is null by default.

提交回复
热议问题