Get Int Value From Enum In C# - Convert Enum To Int In C#

Sameer Saini June 12, 2022
Get Int Value From Enum In C# - Convert Enum To Int In C#

In this blog post, we are going to learn how we can get an Int value from Enum in C#

As we know, An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.

We can get the integral value of an enum and can use it inside our C# application.





Get Int value from Enum in C# Using TypeCast

The best most efficient way to get the integer value from enum in C# is to use the TypeCast operator.

Let's see this in an example.



namespace ConsoleApp { public enum Month { Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, June = 6, Jul = 7, Aug = 8, Sep = 9, Oct = 10, November = 11, December = 12 } public class Program { public static void Main() { int valueOfEnumInInt = (int)Month.Oct; Console.WriteLine(valueOfEnumInInt); } } }



// Int value for Enum Month.Oct is: 10


From the above code example, we can see that it's very easy to get the int value from enums because the underlying type of an enum is an int.

We used the TypeCast operator and used the (int) to TypeCast enum to an int value.

Using this simple TypeCast operator we get int value for any enum in C#.