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

xyz picture xyz · Feb 9, 2009 · Viewed 192.4k times · Source

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.

Answer

James Curran picture James Curran · Feb 9, 2009

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.