Multiple ways to Convert Enum to/from Integer in C#(Examples)

This tutorial covers the following aspects in C#:

  • Converting Integers to Enum Constants
  • Parsing Enums to Integers in C#

An Enum is a custom special class type designed to hold a group of constant values, which can be either strings or numbers.

public enum DAYS : int{
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7,
}

In C#, the Enum type supports constants that can be mapped to Integer, Short, Bytes, and String.

In the example provided, Enum constants are specifically mapped with integers.

On the other hand, Int is a native predefined type in C# used to store numeric values.

Automatic conversion is not feasible due to the distinct data types.

Automatic conversion is not feasible due to the distinct data types.

How to Convert Int to Enum in C#?

There are multiple ways to convert integers to Enum constants in C#:

  • Use type casting. Sometimes, there is a need to convert integer values to Enum constants. Casting the type for a given value is a simple method to achieve this.

    Enum enum = (Enum) integer ;

    Here is an example for parse Int to Enum in C#.

    using System;
    
    public class Program
    {
        public static void Main()
        {
        int weekNumber= 2;
        var day= (DAYS)weekNumber;
        Console.WriteLine("Day is : {0}", day);
    
        }
    }
    
    public enum DAYS : int
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7,
    }

    Output:

    The day is : Tuesday

    Sometimes, invalid numbers are passed, resulting in an error or unexpected outcome. To verify if a valid integer is parsed to an Enum object, the Enum.IsDefined method is used.

    This method checks whether the number is valid or not, returning true if it is and false otherwise. Enum.IsDefined takes the Enum type and an integer as arguments.

    using System;
    
    public class Program
    {
        public static void Main()
        {
        DAYS days;
        int weekNumber= 1;
        // Check if an integer is mapped to Enum or not
        var res=Enum.IsDefined(typeof(DAYS), weekNumber);
        var res1=Enum.IsDefined(typeof(DAYS), 11);
        Console.WriteLine("{0}", res); // True
        Console.WriteLine("{0}", res1); // False
    
        if(res){
          var day= (DAYS)weekNumber;
          Console.WriteLine("Day is : {0}", day);
        } else {
          Console.WriteLine("Not able to convert integer to Enum");
        }
    
        }
    }

    In the given example, initially, check whether an integer is mapped to an Enum or not. If it is valid, proceed to convert it to an Enum object using casting. This approach helps in avoiding errors and potential issues.

  • use Enum.tryParse()

    Enum.tryParse()takes a string representation of a number and converts it into an enum object.

    using System;
    
    public class Program
    {
        public static void Main()
        {
      DAYS days;
      var weekNumber= "2";
    
      Enum.TryParse(weekNumber.ToString(), out days);
    
        Console.WriteLine("Day is : {0}", days);
    
        }
    }

    This method takes a string representing a number, converts it to a numeric value, and then transforms it into an enum object. Utilizing this method helps in avoiding errors. If the process is successful, it returns an Enum object.

How to Parse Enum to Integer in C#?

There are multiple ways we can convert Enum to an integer in C#

  • Use type casting

    It is easy to convert Enum to an integer using cast. Enum constant cast to int type as given below

        int value = (int) Enum.constant;

    Here is an example

    using System;
    
    public class Program
    {
        public static void Main()
        {
        int number = (int) DAYS.Friday;
        Console.WriteLine("Day number is : {0}", number);
    
        }
    }
    
    public enum DAYS : int
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7,
    }

    Compiled to output:

    The day number is 5
  • Use Convert.ToInt32 function

    The Convert.ToInt32() method converts an enum object to an integer. It takes an enum object as input and returns the corresponding integer.

      DAYS day= DAYS.Friday;
        int number = Convert.ToInt32(day);
        Console.WriteLine("{0}", number); // 5
  • Cast the enum to an object, then recast it to an int type.

    Initially, cast the enum to an object and subsequently convert it back to an int type.

        DAYS day= DAYS.Friday;
        int number = (int)(object)day;