How to cast int to enum in C#


To cast int to enum in C# use the below single line C# code

SampleEnum sample = (SampleEnum)IntVariable;

Let’s go through an example to understand it further.

public enum Days
{
        Monday,  
        Tuesday,  
        Wednesday,  
        Thursday,  
        Friday,  
        Saturday,  
        Sunday
}

int dayInteger = 6;
Days day = (Days) dayInteger;
Console.WriteLine(day.ToString());//Monday

But there is a problem with above int to enum casting, what if the int value does not exists in the C# Enum variable?

int dayInteger = 100;
Days day = (Days) dayInteger;
Console.WriteLine(day.ToString());//100

It will not throw any exception.

So it’s better to check if the int value exists in Enum before casting it to the Enum.

To get the all integer values in C# Enum we can use Enum.GetValues method.

var intValue = 100;
var enumValues = Enum.GetValues(typeof(Days)).Cast<int>().ToList();

if(enumValues.Contains(100)){
  Console.WriteLine("We can Cast int to Enum");  
   Days day = (Days) intValue;
}else{
  Console.WriteLine("Cannot Cast int to Enum");
}

Spread the word

Get free link to download 900+ Material Icons
Avatar
Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Previous