The default value for an enum is zero. If an enum does not define an item with a value of zero, its default value will be zero.
One consideration for the use case where a default enum is desired is to use a nullable variable. When a null is received, it can be translated in the correct part of the code into the default, and this default doesn't have to be known in the rest of the code (that just passes along a null).
The default value of an enumeration type E is the value produced by expression (E)0 , even if zero doesn't have the corresponding enum member. You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices.
In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.
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.
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'
If zero doesn't work as the proper default value, you can use the component model to define a workaround for the enum:
[DefaultValue(None)]
public enum Orientation
{
None = -1,
North = 0,
East = 1,
South = 2,
West = 3
}
public static class Utilities
{
public static TEnum GetDefaultValue<TEnum>() where TEnum : struct
{
Type t = typeof(TEnum);
DefaultValueAttribute[] attributes = (DefaultValueAttribute[])t.GetCustomAttributes(typeof(DefaultValueAttribute), false);
if (attributes != null &&
attributes.Length > 0)
{
return (TEnum)attributes[0].Value;
}
else
{
return default(TEnum);
}
}
}
and then you can call:
Orientation o = Utilities.GetDefaultValue<Orientation>();
System.Diagnostics.Debug.Print(o.ToString());
Note: you will need to include the following line at the top of the file:
using System.ComponentModel;
This does not change the actual C# language default value of the enum, but gives a way to indicate (and get) the desired default value.
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.")
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With