views:

338

answers:

3

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?

+10  A: 

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

int value = (int)System.Net.HttpStatusCode.Forbidden;
JaredPar
what is an example of a minority that cannot be simply cast?
Jimmy
@Jimmy, any enum which has a different base type. In those cases you just need to cast to the appropriate base type (uint32, etc)...
JaredPar
I might add that you can get the enum's underlying type by using the static Enum.GetUnderlyingType(typeof(MyEnum)) method.
Yoopergeek
+1  A: 

You can just cast it to an integer! So, int code = (int)enumVariable

Erich
A: 

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

Dieter G