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
?
orNullable<T>
to define a nullable value type, which is a different concept, so you can useHttpStatusCode
to get a nullable such enum, but the enum itself cannot benull
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.0
which doesn't map directly to anHttpStatusCode
.Enums
are value types and can not be null. You can use theNullable<T>
to wrap your enum. In your case you can probably just define 0 to represent the incorrect state.null
and see if it works. Where's the curiosity to see for yourself?