How to get the int value from an enum in C#

Simply type cast the enum to an int.

enum Side
int value = (int)Side.Left;

The above will work for the vast majority of enums as the default underlying type for an enum is int.

However, enums can have different underlying types. If an enum is declared as a uint, long, or ulong, it should be cast to the type of the enum. Here's an example for long:

enum Planet : long
   Mars = 137726861, 
   Jupiter = 2039382280, 
   Saturn = 2647483649 
long value = (long)Planet.Saturn;

Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023

Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.