Skip to main content

Just cast the enum eg, e.g.

int something = (int) Question.Role; 

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is intint.

However, as cecilphillip pointscecilphillip points out, enums can have different underlying types. If an enum is declared as an uinta uint, longlong, or ulong thenulong, it should be cast to the type of the enum.

enum; e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 

you should use

long something = (long)StarsInMilkyWay.Wolf424B; 

Just cast the enum eg.

int something = (int)Question.Role; 

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However as cecilphillip points out, enums can have different underlying types. If an enum is declared as an uint, long or ulong then it should be cast to the type of the enum.

e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 

you should use

long something = (long)StarsInMilkyWay.Wolf424B; 

Just cast the enum, e.g.

int something = (int) Question.Role; 

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However, as cecilphillip points out, 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; e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 

you should use

long something = (long)StarsInMilkyWay.Wolf424B; 
Further detail
Source Link
Tetraneutron
  • 34k
  • 4
  • 27
  • 22

Just cast the enum eg.

int something = (int)Question.Role; 

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However as cecilphillip points out, enums can have different underlying types. If an enum is declared as an uint, long or ulong then it should be cast to the type of the enum.

e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 

you should use

long something = (long)StarsInMilkyWay.Wolf424B; 

Just cast the enum eg.

int something = (int)Question.Role; 

Just cast the enum eg.

int something = (int)Question.Role; 

The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.

However as cecilphillip points out, enums can have different underlying types. If an enum is declared as an uint, long or ulong then it should be cast to the type of the enum.

e.g. for

enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649}; 

you should use

long something = (long)StarsInMilkyWay.Wolf424B; 
Source Link
Tetraneutron
  • 34k
  • 4
  • 27
  • 22

Just cast the enum eg.

int something = (int)Question.Role;