C# example

How to convert an int to an enum in C#

7 min read Updated Oct 2026 Runs in an isolated runtime
Quick answer

Cast it: (OrderStatus)2 gives OrderStatus.Shipped, and (int)OrderStatus.Shipped gives 2 back. The cast never checks the value: (OrderStatus)42 compiles, runs and prints 42. So when the int comes from outside your program, check it with Enum.IsDefined before you trust it.

A C# enum is a named set of constants on top of an integral type, int unless you pick another one. Converting between the two is an explicit cast in each direction, with no parsing and no lookup. That makes it fast, and it also means the compiler won't stop you from creating a value that matches no member. The examples below use one small OrderStatus enum with explicit numbers, the way you would declare one whose values are stored in a database. They cover validating the int, getting the number and the name back, and [Flags] enums where one value holds several members. Strings are a separate article (convert a string to an enum), and so is looping through an enum. Each example runs on this page: hit Run, then edit the code and run it again.

1Cast the int to the enum typeRecommended

(OrderStatus)code reinterprets the number as an OrderStatus. It works from any integral type, so a long or short read from a database column casts the same way. When generic or reflection code only has a Type object, Enum.ToObject(type, value) does the same conversion and returns the enum boxed as object. Neither one checks that the number belongs to a member.

Program.cs

Output

Prints Shipped and True, then Cancelled for the long 9. Enum.ToObject prints Paid (OrderStatus): the boxed value really is an OrderStatus, not an int. Then the catch: (OrderStatus)42 is a legal value that prints 42 and equals no member (False). The last line prints Pending Pending, because the literal 0 converts to any enum without a cast and default(OrderStatus) is always 0. That is why an enum should have a member worth 0, such as Pending, None or Unknown: fields and array elements of the enum type start out as 0.

2Check the value with Enum.IsDefined

For a number from a request, a file or a database, check it before you use it. Enum.IsDefined(value) returns true only when the value is a named member. The generic overload shown here needs .NET 5 or later; on .NET Framework, use Enum.IsDefined(typeof(OrderStatus), value). Wrapping it in the .NET Try pattern, like int.TryParse, gives you a helper that never throws and works with any enum.

Program.cs

Output

The loop prints 1 -> Paid, 3 -> Delivered and 9 -> Cancelled, while 4 (a gap in the numbering) and -1 print not defined. The helper prints found Delivered, 7 is not an OrderStatus and found Medium for the byte enum, because it converts with Enum.ToObject before checking. The old non-generic overload is stricter than it looks: Enum.IsDefined(typeof(Level), 2) throws ArgumentException: Enum underlying type and the object must be same type or object must be a String. Type passed in was 'System.Int32'; the enum underlying type was 'System.Byte'. Passing (byte)2 prints True. Don't use IsDefined on a [Flags] enum: combinations fail it (see section 5).

3Get the int value of an enum

The other direction is the same cast: (int)status. C# never converts an enum to a number implicitly, so int code = status; fails with error CS0266: Cannot implicitly convert type 'OrderStatus' to 'int'. An explicit conversion exists (are you missing a cast?). If the enum declares another underlying type, such as enum Level : byte, cast to that type. Casting to a smaller type than the enum uses works but can lose data.

Program.cs

Output

(int)status and Convert.ToInt32(status) both print 2. In string interpolation an enum formats as its name, and the D format gives the number, so the third line is Shipped = 2. Level reports System.Byte and (byte)Level.High is 3. The long enum shows the risk of a narrowing cast: (long)quota prints 10000000000, a plain (int)quota silently prints 1410065408, and only checked catches it with OverflowException: Arithmetic operation resulted in an overflow. The loop prints each member with its number, from 0 Pending to 9 Cancelled.

4Get the enum name from an int value

To turn a number into a member name, cast it and call ToString(), or pass the enum value to Enum.GetName. They differ only on a number that isn't a member: ToString() falls back to the digits, while GetName returns null, which is easier to test for. For a name you know at compile time, nameof(OrderStatus.Delivered) is a constant string with no lookup at all.

Program.cs

Output

Prints Shipped twice. For the non-member 5, Enum.GetName returns null (shown as (null)) while ToString() prints 5. The non-generic Enum.GetName(typeof(OrderStatus), 9) prints Cancelled, and Enum.GetNames lists Pending, Paid, Shipped, Delivered, Cancelled. The format specifiers print Cancelled 9 00000009: X pads to the size of the underlying type, eight hex digits for an int. The generic GetName<TEnum> and GetNames<TEnum> need .NET 5+. For display text such as "Awaiting payment", map members to strings yourself; the string-to-enum article covers that.

5Combine values with a [Flags] enum

When one value should hold several members at once, such as file permissions, give each member its own bit (1, 2, 4, 8, …) and mark the enum [Flags]. You combine members with |, test one with HasFlag or &, and remove one with &= ~. Converting an int is still a cast. What the attribute changes is ToString(), which lists the set flags instead of printing a number.

Program.cs

Output

Read | Write prints Read, Write and is 3 as an int. The cast (Permissions)5 prints Read, Execute, and 7 prints All because a named combination wins. HasFlag(Execute) is True, the & test for Write is False, and after adding Write and removing Execute the value prints Read, Write. Value 6 splits into Write + Execute. Note that Enum.IsDefined((Permissions)3) is False although 3 is a perfectly good combination, so check flags against a mask instead: 3 is valid, but 12 has a bit no member uses, prints 12 and fails the check. Without the attribute, (PlainPermissions)3 prints 3.

6Which should you use?

MethodConvertsNon-member valueBest for
(OrderStatus)nint to enumKept silentlyNumbers your own code produced
Enum.IsDefined + cast (TryToEnum)int to enumReturns falseInput from users, files, APIs, databases
Enum.ToObject(type, n)int to enumKept silentlyGeneric or reflection code with a Type
(int)statusenum to intReturns the numberStoring or sending the value
status.ToString()enum to namePrints the digitsLogs and display
Enum.GetName(status)enum to nameReturns nullTelling members from unknown numbers
[Flags] + HasFlag / &int to a set of membersCheck with a maskValues that combine, like permissions

Frequently asked questions

How do I cast an int to an enum in C#?

Put the enum type in parentheses: OrderStatus status = (OrderStatus)2; gives OrderStatus.Shipped. The cast works from any integral type, and Enum.ToObject(typeof(OrderStatus), 2) does the same when the type is only known at run time. The cast does not check the value, so validate input with Enum.IsDefined first.

How do I get the int value of an enum in C#?

Cast it: (int)OrderStatus.Shipped is 2. There is no implicit conversion, so int code = status; fails with error CS0266: Cannot implicitly convert type 'OrderStatus' to 'int'. Convert.ToInt32(status) also works, and status.ToString("D") gives the number as a string. If the enum is declared with another underlying type, such as : byte or : long, cast to that type.

What happens if I cast an int that is not a defined enum value?

Nothing fails. (OrderStatus)42 is a valid OrderStatus that prints 42, equals no member and casts back to 42. A switch over it falls through to the discard arm _, so keep one. To reject such values, check Enum.IsDefined((OrderStatus)42), which is False, or use a TryToEnum helper that returns false.

How do I get the name of an enum value from an int?

Cast and call ToString(): ((OrderStatus)2).ToString() is "Shipped". Enum.GetName((OrderStatus)2) returns the same, but for a number that is not a member it returns null where ToString() returns the digits, such as "5". On .NET Framework, use Enum.GetName(typeof(OrderStatus), 2). If two members share a value, the documentation only promises one of their names.

What does the [Flags] attribute do on an enum?

It tells .NET that the values are bit fields meant to be combined with |. The main visible change is ToString(): with [Flags], (Permissions)3 prints Read, Write; without it, it prints 3. The attribute does not number the members for you, so give each one its own bit (1, 2, 4, 8 or 1 << n) and add None = 0. Watch out for HasFlag(Permissions.None), which is always True.

Why can I assign 0 to an enum without a cast?

C# allows the constant 0 to convert to any enum type implicitly, so OrderStatus zero = 0; compiles and prints Pending. Any other literal needs a cast: OrderStatus status = 1; fails with error CS0266: Cannot implicitly convert type 'int' to 'OrderStatus'. Because default(OrderStatus) is also 0, give every enum a member with the value 0.

Run it yourself

Open any of these in the full C# editor to tweak, run and share.

C# playground