12

How do i create an enum with a null value

ex:

public enum MyEnum
{
    [StringValue("X")]
    MyX,
    [StringValue("Y")]
    MyY,      
    None
}

where None value is null or String.Empty

Prisoner
  • 27,391
  • 11
  • 73
  • 102
Hélder Gonçalves
  • 3,822
  • 13
  • 38
  • 63

5 Answers5

22

You can try something like this:-

public MyEnum? value= null;

or you can try this:-

 public MyEnum value= MyEnum.None;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
5

use the "?" oeprator for a nullable type !!

public MyEnum? myEnum= null;
shaouari
  • 236
  • 1
  • 3
5

As all the other answers said, you can't have an enum value be null. What you can do is add a Description attribute (like your StringValue) which has a null value.

For instance:

public enum MyEnum
{
    [Description(null)]
    None = 0,
    [Description("X")]
    MyX,
    [Description("Y")]
    MyY
}

You can get the description of the Enum with the following function:

    public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null && attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }

Usage:

MyEnum e = MyEnum.None;
string s2 = GetEnumDescription((MyEnum)e); // This returns null
Blachshma
  • 17,097
  • 4
  • 58
  • 72
4

All enum types are value types and the different members are also derived from member types (the allowed types are byte, sbyte, short, ushort, int, uint, long, or ulong - as documented).

This means that members of an enumeration cannot be null.

One way to deal with a default enum value is to put the None value as the first value and setting it to a negative value (for signed enums) or 0.

public enum MyEnum
{
    [StringValue(null)]
    None = -1,
    [StringValue("X")]
    MyX,
    [StringValue("Y")]
    MyY      
}

You can have a null reference to an enumeration if you declare a nullable instance - myenum?.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • The value None should always have a value of 0. – erikkallen Nov 28 '12 at 12:53
  • 1
    @erikkallen - Why should it always be 0. It could also be -1, it actually could by any value, that cannot actually be a real value in the given situation. – Security Hound Nov 28 '12 at 13:03
  • Because the default initializer of an enum will set the enum to `(MyEnum)0`. [FxCop agrees](http://msdn.microsoft.com/en-us/library/ms182149(v=vs.80).aspx). Perhaps None shouldn't be the default always though, eg. if you have an enum of "negative options", perhaps `All` should be the default value. – erikkallen Nov 28 '12 at 20:35
2

There is no such thing as an "enum with a null value". An enum in C# is just a named integer. In your case, None is just an alias for 2 (for completeness, MyX is 0 and MyY is 1). By default, the underlying data-type of an enum is int, but it can also be other integer-based primitive types of different sizes/sign.

If you want a "null" enum, you would have to use MyEnum?, aka Nullable<MyEnum>.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900