Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from System.Enum to base integer?

I'd like to create a generic method for converting any System.Enum derived type to its corresponding integer value, without casting and preferably without parsing a string.

Eg, what I want is something like this:

// Trivial example, not actually what I'm doing. class Converter {     int ToInteger(System.Enum anEnum)     {         (int)anEnum;     } } 

But this doesn't appear to work. Resharper reports that you can not cast expression of type 'System.Enum' to type 'int'.

Now I've come up with this solution but I'd rather have something more efficient.

class Converter {     int ToInteger(System.Enum anEnum)     {         return int.Parse(anEnum.ToString("d"));     } } 

Any suggestions?

like image 296
orj Avatar asked May 26 '09 01:05

orj


People also ask

Can you cast an enum to an int?

Overview. To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the Convert class.

How do you convert enum to int in Python?

Use the IntEnum class from the enum module to convert an enum to an integer in Python. You can use the auto() class if the exact value is unimportant. To get a value of an enum member, use the value attribute on the member.

Can enum be treated as int?

An Enum value cannot be treated as an int by default because then you would be able to provide any integer and there would be no compile time check to validate that the provided integer does in fact exist as a value in the Enumeration.


2 Answers

If you don't want to cast,

Convert.ToInt32() 

could do the trick.

The direct cast (via (int)enumValue) is not possible. Note that this would also be "dangerous" since an enum can have different underlying types (int, long, byte...).

More formally: System.Enum has no direct inheritance relationship with Int32 (though both are ValueTypes), so the explicit cast cannot be correct within the type system

like image 149
MartinStettner Avatar answered Oct 06 '22 03:10

MartinStettner


I got it to work by casting to an object and then to an int:

public static class EnumExtensions {     public static int ToInt(this Enum enumValue)     {         return (int)((object)enumValue);     } } 

This is ugly and probably not the best way. I'll keep messing with it, to see if I can come up with something better....

EDIT: Was just about to post that Convert.ToInt32(enumValue) works as well, and noticed that MartinStettner beat me to it.

public static class EnumExtensions {     public static int ToInt(this Enum enumValue)     {         return Convert.ToInt32(enumValue);     } } 

Test:

int x = DayOfWeek.Friday.ToInt(); Console.WriteLine(x); // results in 5 which is int value of Friday 

EDIT 2: In the comments, someone said that this only works in C# 3.0. I just tested this in VS2005 like this and it worked:

public static class Helpers {     public static int ToInt(Enum enumValue)     {         return Convert.ToInt32(enumValue);     } }      static void Main(string[] args)     {         Console.WriteLine(Helpers.ToInt(DayOfWeek.Friday));     } 
like image 27
BFree Avatar answered Oct 06 '22 05:10

BFree