This post will discuss how to get the constant integer value from the enum member name in C#.

1. Using Casting

We can get the constant integer value simply by casting the enum member name. This is demonstrated below:

Download  Run Code

 
The default underlying type for an enum is an integer. However, if enum has different underlying types such as uint, short, ushort, long, ulong, etc., it should be cast to the enum’s corresponding type.

2. Using Convert.ChangeType() method

To get the enum’s underlying value, we can use the Convert.GetTypeCode() method. The following code would work for any type of enum.

Download  Run Code

3. Using Object.GetHashCode() method

Another way to get the constant value from the enum member name is by calling the GetHashCode() method on the enum member name. This is demonstrated below:

Download  Run Code

That’s all about getting an integer value from an enum member name in C#.