How to get the numeric value from the Enum?

C#.NetReflectionPropertiesEnums

C# Problem Overview


For example System.Net.HttpStatusCode Enum, I would like to get the HTTP Status Codes instead of the HTTP Status Text. System.Net.HttpStatusCode.Forbidden should return 403 instead of "Forbidden".

How can I extract the value?

C# Solutions


Solution 1 - C#

For the majority of Enum's simply cast to the base type which is int32.

int value = (int)System.Net.HttpStatusCode.Forbidden;

Solution 2 - C#

You can just cast it to an integer!

int code = (int)enumVariable

Solution 3 - C#

System.Convert.ToInt32(response.StatusCode) returns the statusCode number

Solution 4 - C#

I think @JaredPar's answer is great, but as he himself explains it does not always work, so i will offer a complete answer here.

The short answer

Instead of simply casting, use the following code

var value = 
    typeof(System.Net.HttpStatusCode)
        .GetField("value__")
        .GetValue(System.Net.HttpStatusCode.Forbidden);

Console.WriteLine(value); // 403

Now lets elaborate a bit on this...

Numeric value of Enum is not always int (!!!)

As explained in the Docu, > Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying type must be able to represent all the enumerator values defined in the enumeration. An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong. Note that char cannot be used as an underlying type. An enum declaration that does not explicitly declare an underlying type has an underlying type of int.

So, lets imagine you are dealing with an enum that is declared as

enum LongEnum : long 
{ 
    min = long.MinValue, 
    max = long.MaxValue 
}

by applying the solution given by @JaredPar

int value = (int)LongEnum.min;

you will get the following error: > error CS0221: Constant value '-9223372036854775808' cannot be converted to a 'int' (use 'unchecked' syntax to override)

So, in order to get around this, the trick is of course, as you saw above, to use Reflection:

object value =
    typeof(LongEnum)
    .GetField("value__")
    .GetValue(LongEnum.min);

Console.WriteLine(value); // -9223372036854775808

which prints the right value!

But... what is that 'value__' field of enum???

To answer this, i will follow an answer of @Hans Passant: > The JIT compiler needs a definition of a value type that describes its layout when it gets boxed. Most of them are baked into mscorlib, like System.Int32. The enum keyword lets you create a new value type. The compiler must thus provide a definition for it in the metadata. Which is what you are looking at. You'll see static fields for each enumeration member, used by ToString(). And one instance field name value__ that stores the enumeration value. Key point is that this only exists in the boxed version of an enum value.

P.S. ...

Since our solution returns a value of type object, someone would be tempted to modify @JaredPar's answer as follows

Object value = (object)LongEnum.min;

Console.WriteLine(value);

nevertheless, that will just print the member name min.

Solution 5 - C#

you can use a Principled way using .NET CLR functions..

int value = System.Net.HttpStatusCode.Forbidden.GetHashCode();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkurozakuraView Question on Stackoverflow
Solution 1 - C#JaredParView Answer on Stackoverflow
Solution 2 - C#ErichView Answer on Stackoverflow
Solution 3 - C#Dieter GView Answer on Stackoverflow
Solution 4 - C#deczalothView Answer on Stackoverflow
Solution 5 - C#reza akhlaghiView Answer on Stackoverflow