xyz xyz - 1 year ago 81
C# Question

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download