Earn income with your C# skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

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

Simply type cast the enum to an int.


enum Side
{
   Left, 
   Right, 
   Top, 
   Bottom
}
    
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;



Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.3.0