HOWTO · Csharp

How to Convert Int to Enum in C#

Learn how to convert an integer to a C# enum with a cast or parsing, and check whether the result is a declared member.

On this page

To convert an integer to an enum in C#, cast it to the enum type: MyEnum value = (MyEnum)number;. An enum is a value type with named constants and an underlying integral type. A cast converts the underlying value, but it does not check whether that value matches a declared constant. Use Enum.IsDefined when only named members are acceptable; use Enum.Parse or Enum.TryParse when the input is text.

Convert an Integer to an Enum with a Direct Cast

A direct cast is the simplest choice when the integer is already available and you control its value. This complete example defines an enum with the default int underlying type, casts the value 2, and prints the corresponding member name.

using System;

public class Program
{
    public enum MyEnum { Zero = 0, One = 1, Two = 2 }

    public static void Main()
    {
        int number = 2;
        MyEnum value = (MyEnum)number;
        Console.WriteLine($"Converted Enum Value: {value}");
    }
}

The cast does not validate the result. For example, casting 7 produces an enum value with underlying value 7 even though MyEnum declares no member with that value. Check with Enum.IsDefined before casting if your application must accept declared members only.

Parse an Integer String with Enum.Parse

Enum.Parse takes a string. When an integer arrives as text, pass that string to the method and cast its returned object to the target enum type. The example parses the numeric string "1".

using System;

public class Program
{
    public enum MyEnum { Zero = 0, One = 1, Two = 2 }

    public static void Main()
    {
        string input = "1";
        MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum), input);
        Console.WriteLine($"Parsed Enum Value: {value}");
    }
}

Parsing a numeric string can produce an enum value whose underlying number has no declared member. Enum.Parse also reports malformed input by throwing an exception, so use Enum.TryParse when ordinary conversion failure should be handled without exception-based control flow. Parsing success alone is not a membership check.

Check for a Defined Enum Value with Enum.IsDefined

Use Enum.IsDefined to determine whether a value corresponds to a declared enum constant. This example checks both a declared integer value and an undeclared one before casting. Because MyEnum uses its default int underlying type, the boxed int arguments match the enum’s underlying type.

using System;

public class Program
{
    public enum MyEnum { Zero = 0, One = 1, Two = 2 }

    public static void Main()
    {
        int[] candidates = { 2, 7 };

        foreach (int number in candidates)
        {
            if (Enum.IsDefined(typeof(MyEnum), number))
            {
                MyEnum value = (MyEnum)number;
                Console.WriteLine($"{number}: defined as {value}");
            }
            else
            {
                Console.WriteLine($"{number}: not defined");
            }
        }
    }
}

Enum.IsDefined checks for an exact declared constant value. It is suitable for a simple enum with separately declared values; for a [Flags] enum, a valid combination of flags may not itself be declared, so this check can reject combinations that your application intends to allow.

Try Parsing an Integer String with Enum.TryParse

Enum.TryParse returns a Boolean indicating whether the text could be converted to the enum type, and writes the parsed value to its out parameter. Convert an integer to text first when that is the input you have. The example also checks membership separately because successful numeric parsing does not establish that the number is a declared member.

using System;

public class Program
{
    public enum MyEnum { Zero = 0, One = 1, Two = 2 }

    public static void Main()
    {
        int number = 1;
        string input = number.ToString();

        if (Enum.TryParse<MyEnum>(input, out MyEnum value))
        {
            Console.WriteLine($"Parsed: {value}");
            Console.WriteLine($"Declared: {Enum.IsDefined(typeof(MyEnum), value)}");
        }
        else
        {
            Console.WriteLine("Could not parse the input.");
        }
    }
}

For name-based text, Enum.TryParse also accepts the declared member name, such as "Two". Numeric text can parse to an undeclared underlying value, so keep the Enum.IsDefined check when the application requires an exact declared member. Use the ignoreCase overload only when case-insensitive matching of names is appropriate.

Choose the Right Conversion Method

Choose based on the form of the input and the validation you need:

  • Use a direct cast for an integer you already trust and when an undeclared enum value is acceptable.
  • Use Enum.Parse for text when a failed parse should be handled as an exception.
  • Use Enum.TryParse for text when you want a Boolean result for conversion success.
  • Use Enum.IsDefined before or after conversion when only declared enum constants are acceptable.

Neither parsing method guarantees that a numeric value names a declared constant. For untrusted or dynamic input, try parsing and then check Enum.IsDefined if your application accepts only declared members. For [Flags] enums, define and apply the validation rules for allowed combinations rather than relying only on Enum.IsDefined.