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

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

    One more way that might be helpful - to use some kind of "alias". For example:

    public enum Status
    {
        New = 10,
        Old = 20,
        Actual = 30,
    
        // Use Status.Default to specify default status in your code. 
        Default = New 
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 08:28

    The default value of any enum is zero. So if you want to set one enumerator to be the default value, then set that one to zero and all other enumerators to non-zero (the first enumerator to have the value zero will be the default value for that enum if there are several enumerators with the value zero).

    enum Orientation
    {
        None = 0, //default value since it has the value '0'
        North = 1,
        East = 2,
        South = 3,
        West = 4
    }
    
    Orientation o; // initialized to 'None'
    

    If your enumerators don't need explicit values, then just make sure the first enumerator is the one you want to be the default enumerator since "By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1." (C# reference)

    enum Orientation
    {
        None, //default value since it is the first enumerator
        North,
        East,
        South,
        West
    }
    
    Orientation o; // initialized to 'None'
    
    0 讨论(0)
  • 2020-12-04 08:29

    The default for an enum (in fact, any value type) is 0 -- even if that is not a valid value for that enum. It cannot be changed.

    0 讨论(0)
  • 2020-12-04 08:29

    The default value for an enumeration type is 0:

    • "By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1."

    • "The value type enum has the value produced by the expression (E)0, where E is the enum identifier."

    You can check the documentation for C# enum here, and the documentation for C# default values table here.

    0 讨论(0)
提交回复
热议问题