Introduction: Why enum without int is like coffee without caffeine?
\n\nImagine 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.
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?
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.
1. How enum works in C#: A quick primer
\n\nBefore 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:
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\nThis is important! If your enum uses byte or long, simply casting to int can cause an error or data loss.
2. Method #1: Explicit cast — the classic
\n\nThe 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.
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\nPros:
\n- \n
- Maximum performance — the compiler generates a single IL instruction. \n
- Simplicity and readability. \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
longorbyte(requires a double cast). \n
When to use: in 90% of cases when your enum is standard (: int).
3. Method #2: Convert.ToInt32 — the universal soldier
\n\nThe 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.
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\nPros:
\n- \n
- Works with any underlying type (
byte,short,long, etc.). \n - Doesn't require explicit knowledge of the enum type. \n
Cons:
\n- \n
- Slower than a direct cast (virtual method call). \n
- Can throw an
InvalidCastExceptionif a non-enum is passed. \n
When to use: when writing generic code or working with an enum whose underlying type is unknown at compile time.
\n\n4. Method #3: GetHashCode() — a dangerous trick (don't do this!)
\n\nOnline, you might find the "brilliant" advice to use GetHashCode(). Yes, technically it