5

Just wondering if an enum can be null in C#?

Specifically I'm interested in the HttpStatusCode implementation of enum in C#.

What will happen if I will not assign a value to a variable of the HttpStatusCode type? What will happen if I will try to assign a value that is not expected?

6
  • 3
    Yes, if you make it nullable. Jan 8, 2019 at 10:08
  • 6
    Enums by themselves cannot be null, because they are value types. You can use ? or Nullable<T> to define a nullable value type, which is a different concept, so you can use HttpStatusCode to get a nullable such enum, but the enum itself cannot be null alone. However, Nullable<T> is a wrapper which means that the type now is something else, it is a "bearer of data", so a nullable enum is actually "something that can be null, or hold an enum value", itself it is no longer an enum. Jan 8, 2019 at 10:09
  • 1
    If you don't assign it a value, it will be 0 which doesn't map directly to an HttpStatusCode.
    – DavidG
    Jan 8, 2019 at 10:10
  • Enums are value types and can not be null. You can use the Nullable<T> to wrap your enum. In your case you can probably just define 0 to represent the incorrect state. Jan 8, 2019 at 10:13
  • 2
    create an enum and assign it null and see if it works. Where's the curiosity to see for yourself?
    – bolov
    Jan 8, 2019 at 10:14

3 Answers 3

5

From MSDN:

Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example. C#

enum Day : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

Since these are only value types, the enum itself cannot be null.

Of course you can wrap it in a Nullable, which allows value types to behave "nullable", but technically, Nullable<T> is a generic struct type (non-nullable) and not an enum itself, it's just a wrapper as by definition:

public struct Nullable<T> where T : struct

You might wonder: how can a struct be null? See this answer for an explanation:

Nullable<bool> b = new Nullable<bool>();

Basically, it's never null. It a compiler trick to guarantee that the values like e.g.: HasValue are always available.

3

HttpStatusCodes Enum

In your particular question about HttpStatusCodes.

What is actually happening is that the "code reader friendly" enum such as StatusCodes.400NotFound is just a representation of an integer value of 400. You can just manually use an integer value as the argument if you like, but then someone else reading your code may not understand the HTTP status code.

For example, if I just wrote in my code the status code422, is it easy to read / understand? Probably not a good idea. Someone reading your code will have a better chance if you use StatusCode.422UnprocessableEntity.

What are the valid HTTP status codes?
If you are sending back an HTTP response, you can designate any of the integer values listed here... https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Unassigned or default behavior(s)?
Without knowing what method or what server you are using, your question about "what happens if it is unassigned". The usual answer is that the server will respond with a 200 (OK) status code as default.

Using non-standard response codes
It really depends on what you are doing, but using response code values that are not part of the standard (ex. -999) may (or may not) error, but you should avoid that as it is not a supported standard.

Nullable Enum

For HttpStatusCodes in all cases (I have ever experienced) you cannot assign a null value to the HttpResponse as the property type is integer.

Note that what I am saying is specific to assigning a status code value to a HttpResponse object. You can read the other answers about the generic question of nullable enums.

2

Not unless you make your variable of a Nullable<MyEnum> (or MyEnum?) type.

Under the hood, enums are integers. When an enum is uninitialized, it has the default(int) value, which is 0.

Edit: other underlying types are possible, but the default is int. Whichever underlying type you use, an uninitialized enum will always be the default value of the underlying type.

When you don't assign integer values to your enum, the first option will be set to 0 and therefore be the default.

public enum MyEnum { Foo, Bar, Baz }

MyEnum myEnum;

if(myEnum == MyEnum.Foo) //true 
if(myEnum == MyEnum.Bar) //false
if(myEnum == MyEnum.Baz) //false

When you do assign integer values to your enum, and there is no 0 (or unassigned) value, then your uninitialized enum will not match any of the options.

public enum MyEnum { Foo = 1, Bar = 2, Baz = 3 }

MyEnum myEnum;

if(myEnum == MyEnum.Foo) //false
if(myEnum == MyEnum.Bar) //false
if(myEnum == MyEnum.Baz) //false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.