Convert int to enum in C#


Here you will learn how to convert Int to Enum in C#.

Convert int to Enum by Type Casting

You can explicitly type cast an int to a particular enum type, as shown below.

Example: Type Cast int to Enum
public enum Week
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

class Program
{
    static void Main(string[] args)
    {
        int i = 2, j = 6, k = 10;
		Week day1, day2, day3;
		
		day1 = (Week)i; //Wednesday
		day2 = (Week)j; //Sunday
		day3 = (Week)k; //10
    }
}

Convert int to Enum using Enum.ToObject() Method

Use the Enum.ToObject() method to convert integers to enum members, as shown below.

Example: Convert int to Enum using Enum.ToObject()
int i = 2, j = 6, k = 10;
Week day1, day2, day3;
		
day1 = (Week)Enum.ToObject(typeof(Week), i); //Wednesday
day2 = (Week)Enum.ToObject(typeof(Week), j); //Sunday
day3 = (Week)Enum.ToObject(typeof(Week), k); //10