Initial Value of an Enum

Solution 1:

Default value for enum types is 0 (which is by default, the first element in the enumeration). Fields of a class will be initialized to the default value.

If you need to represent an unknown value in the enum, you can add an element Unknown with value 0. Alternatively, you could declare the field as Nullable<MyEnum> (MyEnum?).

Solution 2:

Enums are a value type, like ints. You need to make it nullable so as not to default to the first (or 0-defined) enum member.

public class MyClass
{
   public EnumDeliveryAction? DeliveryAction { get; set;}
}

Solution 3:

Enum fields are initialized as zero; an if you don't specify values in an enum, they start at zero (Email = 0, SharePoint=1, etc).

Thus by default any field you do initialize yourself will be Email. It is relatively common to add None=0 for such cases, or alternatively use Nullable<T>; i.e.

/// <summary>
/// All available delivery actions
/// </summary>
public enum EnumDeliveryAction
{
    /// <summary>
    /// Not specified
    /// </summary>
    None,

    /// <summary>
    /// Tasks with email delivery action will be emailed
    /// </summary>
    Email,

    /// <summary>
    /// Tasks with SharePoint delivery action 
   /// </summary>
   SharePoint
}

You should also be sure to never treat your last expected value as a default; i.e.

switch(action) {
    case EnumDeliveryAction.Email; RunEmail(); break;
    default: RunSharePoint(); break;
}

this should be:

switch(action) {
    case EnumDeliveryAction.Email; RunEmail(); break;
    case EnumDeliveryAction.SharePoint; RunSharePoint(); break;
    default: throw new InvalidOperationException(
          "Unexpected action: " + action);
}

Solution 4:

Best practice (as advised by Code Analysis) is to always have a default value in your enums, which represent an unset value.

So in your case, you might have:

public enum EnumDeliveryAction
   {

    /// <summary>
    /// Default value
    /// </summary>
    NotSet,

    /// <summary>
    /// Tasks with email delivery action will be emailed
    /// </summary>
    Email,

    /// <summary>
    /// Tasks with SharePoint delivery action 
   /// </summary>
   SharePoint
  }

As an aside, you shouldn't prefix the name of the enum with Enum. You might consider changing to:

public enum DeliveryAction;

Solution 5:

Enums are value types. Value types cannot be null and are initialized to 0.

Even if your enum does not have a 0, enum variables will be initialized to 0.

public enum SomeEnum
{
    A = 1,
    B = 2
}

(later)

SomeEnum x = default(SomeEnum);
Console.WriteLine(x);

Outputs - 0


Some answerers are advocating using Nullable<T> to match your initialization expectations. Becareful with this advice since Nullable<T> is still a value type and has different semantics than reference types. For example, it will never generate a null reference exception (it's not a reference).

SomeEnum? x = default(SomeEnum?);

if (x == null)
{
    Console.WriteLine("It's null!");
}
if (x > SomeEnum.B)
{
}
else
{
    Console.WriteLine("This runs even though you don't expect it to");
}