Initial Value of an Enum

前端 未结 10 559
陌清茗
陌清茗 2020-12-08 01:51

I have a class with a property which is an enum

The enum is

/// 
/// All available delivery actions
/// 
public enum E         


        
相关标签:
10条回答
  • 2020-12-08 02:13

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 02:20

    By default only reference types are nullable types. If you want a variable to allow nulls you have to define it as nullable using the "?" character (for this you need C# 2.0 or up).

    enum MyEnum
    {
        ValueOne,
        ValueTwo
    }
    

    and in your class

    MyEnum? myvariable = null;
    
    0 讨论(0)
  • 2020-12-08 02:26

    You can start your enums at any value (such as 1), but when they represent a lookup value in a Database, you generally want them to match up.

    I generally declare the first Enum as None ( = 0) when it makes sense to do so, as per the .Net Framework Design guidelines.

    0 讨论(0)
  • 2020-12-08 02:28

    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;
    
    0 讨论(0)
  • 2020-12-08 02:28

    My C++ teacher in college (11 years ago) told me that the linker replaces enum with their actual type:

    typedef static const int enum;

    Thus, any time you write something like enum MY_VAL = 5;, you could easily replace with static const int MY_VAL = 5; (but that just makes your code longer...).

    Anyway, the default value of any int is 0.

    0 讨论(0)
  • 2020-12-08 02:29

    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;}
    }
    
    0 讨论(0)
提交回复
热议问题