How to get an integer value (int) from an enum in C#?
Hello everyone! I just started learning C# and encountered this problem. I have an enum that contains several order statuses:
```csharp
enum OrderStatus
{
New = 1,
Processing = 2,
Shipped = 3,
Delivered = 4,
Cancelled = 5
}
```
I need to save the status value to a database as an integer (int). For example, if the status is "Processing", I want to get the number 2. I tried simply assigning the enum variable to an int, but the compiler complains about implicit conversion:
```csharp
OrderStatus status = OrderStatus.Processing;
int statusValue = status; // Error: Cannot implicitly convert type 'OrderStatus' to 'int'
```
Please advise how to properly convert an enum to int? Are there different ways? And what to do if the enum has a different base type?
No answers yet.
Log in to leave an answer
Blogs