How to Get int from enum in C#: From Beginner to Pro

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why enum without int is like coffee without caffeine?

\n\n

Imagine this: you're writing cool C# code, using enumerations (enum) to store order statuses, days of the week, or access levels. Everything is clean and readable. But suddenly a task arises: save this value to a database, send it via an HTTP API, or just output it to a log. And here you hit a wall — you need to convert the enum to an int.

\n\n

This isn't just a textbook "wish". According to Stack Overflow statistics, the question "Get int value from enum in C#" gets hundreds of thousands of views. Even experienced developers sometimes get confused: casting? Convert.ToInt32? What if the underlying integer type isn't int?

\n\n

In this article, we'll cover all legitimate ways to get an integer value from an enum in C#. Starting from simple type casting to advanced tricks with Unsafe.As (yes, that exists too!). You'll learn which method is faster, which is safer, and which is best avoided in production.

\n\n

1. How enum works in C#: A quick primer

\n\n

Before converting, let's recall the basics. In C#, enum is a value type that inherits from System.Enum, and its underlying type defaults to int. But you can explicitly specify another type:

\n\n
public enum Status : byte\n{\n    New = 0,\n    Active = 1,\n    Closed = 2\n}\n\npublic enum Priority : long\n{\n    Low = 10,\n    Medium = 50,\n    High = 100\n}
\n\n

This is important! If your enum uses byte or long, simply casting to int can cause an error or data loss.

\n\n

2. Method #1: Explicit cast — the classic

\n\n

The most obvious and fastest way is to use the type casting operator. It works when you know for sure that the underlying type is int.

\n\n
enum Day { Monday = 1, Tuesday = 2, Wednesday = 3 }\n\nDay today = Day.Tuesday;\nint dayValue = (int)today; // Result: 2\n\nConsole.WriteLine(dayValue); // Outputs: 2
\n\n

Pros:

\n
    \n
  • Maximum performance — the compiler generates a single IL instruction.
  • \n
  • Simplicity and readability.
  • \n
\n

Cons:

\n
    \n
  • If the underlying type is not int, the compiler will throw an error or warning.
  • \n
  • Doesn't work with enums defined with long or byte (requires a double cast).
  • \n
\n\n

When to use: in 90% of cases when your enum is standard (: int).

\n\n

3. Method #2: Convert.ToInt32 — the universal soldier

\n\n

The Convert.ToInt32() method is a Swiss Army knife for conversion. It takes an object and tries to convert it to int via the IConvertible interface.

\n\n
enum Status { New = 0, Active = 1, Closed = 2 }\n\nStatus current = Status.Active;\nint statusCode = Convert.ToInt32(current); // Result: 1\n\nConsole.WriteLine(statusCode); // 1
\n\n

Pros:

\n
    \n
  • Works with any underlying type (byte, short, long, etc.).
  • \n
  • Doesn't require explicit knowledge of the enum type.
  • \n
\n

Cons:

\n
    \n
  • Slower than a direct cast (virtual method call).
  • \n
  • Can throw an InvalidCastException if a non-enum is passed.
  • \n
\n\n

When to use: when writing generic code or working with an enum whose underlying type is unknown at compile time.

\n\n

4. Method #3: GetHashCode() — a dangerous trick (don't do this!)

\n\n

Online, you might find the "brilliant" advice to use GetHashCode(). Yes, technically it

Blogs

Book Recommendations