How to cast int to enum C#

In this post, we will learn How to cast int to enum in C# with examples. The definition of an Enum or Enumeration is “An enumeration or enum is a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members. We will also cover cast int to enum out of range, C# cast int to enum invalid value these errors occur when we have passed an invalid enum value that does not exist in our enum

1. How to cast int to enum c#


In this C# example, we will understand how to cast int to enum c#, We will use the following syntax. In the below code We are using typecast to cast int to enum c#. We can use any of them in .net core also. We will pass the enum value it automatically cast enum to int

//Way 1:

Enumname variableName = (Enumname)Intvalue;
#example
MyDevenum val = (MyDevenum)num;

//way 2:

if (Enum.IsDefined(typeof(Enumname ), Intvalue))
{
    return (Enumname ) Intvalue;
}

//Way 3:
if (Enum.IsDefined(typeof(Enumname ), Intvalue))
{
 return (Enumname) Enum.ToObject(typeof(Enumname z), Intvalue);
}
using System;
public class Program {
    public enum MyDevenum
{
    Dev = 0,
    data = 1,
    Tech =2,
}
public static void Main() {
int num = 2;
MyDevenum val = (MyDevenum)num;
Console.WriteLine("Intger to Enum:{0}",val);
   }
}

Output

Intger to Enum:Tech

2. C# Cast int to enum out of range


We are asking the user to input the enum constant that has to convert into an enum. The user has entered the valid enum constant 2 and by using typecasting it is converted into a valid enum member.

On the second try, the user has entered the enum value 4. The if statement is used to check if the entered value exists using Enum.IsDefined(typeof(MyDevenum), num) when it does not exist it throws an exception “System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.“. Whenever a value is valid it will cast int to enum.

using System;
public class Program {
    public enum MyDevenum
{
    Dev = 0,
    data = 1,
    Tech =2,
}
public static void Main() {
Console.Write("Please enter a number: ");
string inputnum = Console.ReadLine();
int num = Convert.ToInt32(inputnum);
if(!Enum.IsDefined(typeof(MyDevenum), num))
     throw new ArgumentOutOfRangeException();
else
{
MyDevenum val = (MyDevenum)num;
Console.WriteLine("Intger to Enum:{0}",val);
}
   }
}

Output

Specified argumePlease enter a number: 2
Intger to Enum:Tech.

Please enter a number: 4
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

3. C# cast int to enum invalid value


This error is also raised when we entered the invalid enum constant value while converting int to enum. Whenever the value is valid it will be converted from int to enum.

using System;
public class Program {
    public enum MyDevenum
{
    Dev = 0,
    data = 1,
    Tech =2,
}
public static void Main() {
Console.Write("Please enter a number: ");
string inputnum = Console.ReadLine();
int num = Convert.ToInt32(inputnum);
if(!Enum.IsDefined(typeof(MyDevenum), num))
     throw new ArgumentOutOfRangeException();
else
{
MyDevenum val = (MyDevenum)num;
Console.WriteLine("Intger to Enum:{0}",val);
}
   }
}

Output

Please enter a number: 5
Unhandled Exception:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

4. How to check enum value exists


Sometimes we have a requirement To check the enum value is exist. The Enum.IsDefined() method is used to check value exists in enum and it returns a Boolean value. When the value exists, it returns True else returns False. Let us understand with the below example.

The value 2 exists in the enum the return value is True, However, the value 4 does not exist in Enum, So False is returned.

using System;
public class Program {
    public enum MyDevenum
{
    Dev = 0,
    data = 1,
    Tech =2,
}
public static void Main() {

bool enum2IsDefined = Enum.IsDefined(typeof(MyDevenum), 2);
bool enum3IsDefined = Enum.IsDefined(typeof(MyDevenum), 4);

Console.WriteLine("Value2 is exist:{0}",enum2IsDefined);
Console.WriteLine("Vale3 is Exist:{0}",enum3IsDefined);
   }
}

Output

Value2 is exist:True
Vale3 is Exist:False

Summary

In this post, we have learned How to cast int to enum c# with examples. We have also covered cast int to enum out of range, C# cast int to enum invalid value these errors occur when we have passed an invalid enum value that does not exist in our enum. This post also includes How to check whether the enum value exists or not.