Initial Enum

I have a class with a property that is an enumeration

Listing

/// <summary> /// All available delivery actions /// </summary> public enum EnumDeliveryAction { /// <summary> /// Tasks with email delivery action will be emailed /// </summary> Email, /// <summary> /// Tasks with SharePoint delivery action /// </summary> SharePoint } 

When I create an instance of this class, NOWHERE in the code, I specify the value of the enum field, but by default it refers to the first element in the list of enumerations, and not to the null value, is this enumeration Work? How can I guarantee that an enumeration gets some kind of null value, if it is not set, I do not want it to be the first value in the enumeration by default.

+52
enums c #
Jul 22 '09 at 13:49
source share
10 answers

The default value for enum is 0 (by default, this is the first element of the enumeration). Class fields will be initialized by default.

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

+101
Jul 22 '09 at 13:51
source share

Enumerations are a type of value, for example, ints. You need to make it null so as not to use the first (or 0-defined) member of the enum by default.

 public class MyClass { public EnumDeliveryAction? DeliveryAction { get; set;} } 
+8
Jul 22 '09 at 13:52
source share

Enum fields are initialized to zero; a, if you do not specify values ​​in the enumeration, they start from zero ( Email = 0 , SharePoint=1 , etc.).

Thus, by default, any field that you initialize will be Email . For such cases, it is relatively convenient to add None=0 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 must also be sure that you will never handle your last expected default value; i.e.

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

it should be:

 switch(action) { case EnumDeliveryAction.Email; RunEmail(); break; case EnumDeliveryAction.SharePoint; RunSharePoint(); break; default: throw new InvalidOperationException( "Unexpected action: " + action); } 
+7
Jul 22 '09 at 13:53
source share

Best practice (as described in code analysis) is to always have a default value in your enums, which are an undefined value.

So in your case you can:

 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 } 

Aside, you should not prefix the name enum with Enum. You might want to switch to:

 public enum DeliveryAction; 
+6
Jul 22 '09 at 13:56
source share

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

Even if your enum does not have 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 responders recommend using Nullable<T> to meet initialization expectations. Be careful with this tip, because Nullable<T> is still a value type and has different semantics than reference types. For example, it will never throw a null reference exception (this is not a link).

 SomeEnum? x = default(SomeEnum?); if (x == null) { Console.WriteLine("It null!"); } if (x > SomeEnum.B) { } else { Console.WriteLine("This runs even though you don't expect it to"); } 
+3
Jul 22 '09 at 2:00
source share

You can run your enums with any value (e.g. 1), but when they represent the search value in the database, you usually want them to match.

I usually declare the first Enum as None (= 0) when it makes sense to do this, as recommended by the .Net Framework Design.

+1
Jul 22 '09 at 13:55
source share

The traditional aproach to add null values ​​to values ​​that usually don't have is to declare your variable as a null type, i.e.:

 EnumDeliveryAction? action=null; 
0
Jul 22 '09 at 13:54
source share

I suggest having None = 0 as your first enum value. Make it explicit, then you know exactly what it is.

0
Jul 22 '09 at 13:55
source share

By default, only reference types are types with a null value. If you want a variable to allow nulls, you must define it as nullable with the parameter ?? (for this you need C # 2.0 or higher).

 enum MyEnum { ValueOne, ValueTwo } 

and in your class

 MyEnum? myvariable = null; 
0
Jul 22 '09 at 13:56
source share

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

typedef static const int enum;

Thus, every time you write something like enum MY_VAL = 5; , you can easily replace it with static const int MY_VAL = 5; (but it just makes your code last longer ...).

In any case, the default value for any int is 0.

0
May 26 '17 at 20:17
source share



All Articles