tags:

views:

4338

answers:

5

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 numbers required might be set in stone for whatever reason, and it'd be handy to still have control over the default.

enum Orientation
{
    None = -1,
    North = 0,
    East = 1,
    South = 2,
    West = 3
}

Orientation o; // Is 'North' by default.
+21  A: 

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.

James Curran
Rephrase the question to ask that the default value for an int be 42.Think how daft that sounds... Memory is *blanked* by the runtime before you get it, enums are structs remember
ShuggyCoUk
This must be wrong. Way wrong. The default for an enum is the first value in the enum. Which might be non-zero.
Andrei Rinea
Oh shit, I was wrong - just ran a simple app and it seems I am sooo wrooong. Sorry.
Andrei Rinea
+6  A: 

An enum's default is whatever enumeration equates to zero. I don't believe this is changeable by attribute or other means.

(MSDN says: "The default value of an enum E is the value produced by the expression (E)0.")

Joe
Strictly, there doesn't even need to be an enumeration with this value. Zero is still the default.
Marc Gravell
+1  A: 

Actually an enums default its the first element in the Enum whose value is 0

So for example:

public enum Animals { Cat, Dog, Pony = 0, }

Animals animal; (its value will actually be Cat not Pony unless you assign a non zero value to Cat.

Cian
+1  A: 

You can't, but if you want, you can do some trick. :)

    public struct Orientation
    {
        ...
        public static Orientation None = -1;
        public static Orientation North = 0;
        public static Orientation East = 1;
        public static Orientation South = 2;
        public static Orientation West = 3;
    }

usage of this struct as simple enum.
where you can create p.a == Orientation.East (or any value that you want) by default
to use the trick itself, you need to convert from int by code.
there the implementation:

        #region ConvertingToEnum
        private int val;
        static Dictionary<int, string> dict = null;

        public Orientation(int val)
        {
            this.val = val;
        }

        public static implicit operator Orientation(int value)
        {
            return new Orientation(value - 1);
        }

        public static bool operator ==(Orientation a, Orientation b)
        {
            return a.val == b.val;
        }

        public static bool operator !=(Orientation a, Orientation b)
        {
            return a.val != b.val;
        }

        public override string ToString()
        {
            if (dict == null)
                InitializeDict();
            if (dict.ContainsKey(val))
                return dict[val];
            return val.ToString();
        }

        private void InitializeDict()
        {
            dict = new Dictionary<int, string>();
            foreach (var fields in GetType().GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                dict.Add(((Orientation)fields.GetValue(null)).val, fields.Name);
            }
        } 
        #endregion
Avram
+1  A: 

The default value of any enum is zero. So if you want 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 SexualOrientation
{
    None = 0, //default value since it has the value '0'
    North = 1,
    East = 2,
    South = 3,
    West = 4
}

SexualOrientation so; // 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 SexualOrientation
{
    None, //default value since it is the first enumerator
    North,
    East,
    South,
    West
}

SexualOrientation so; // initialized to 'None'
Hermann