How to get int value from enum in C#? Converting enum to number
Hello everyone! I just started learning C# and encountered a problem. I have an enum that describes order statuses:
```csharp
public enum OrderStatus
{
Created,
Processing,
Shipped,
Delivered,
Cancelled
}
```
I need to save the order status to the database as an integer (int). For example, `OrderStatus.Created` should become 0, `OrderStatus.Processing` — 1, and so on. But when I try to simply assign the value:
```csharp
int statusCode = OrderStatus.Created;
```
The compiler gives an error: "Cannot implicitly convert type 'OrderStatus' to 'int'". I understand that enum is a separate type, not just a number. But how do I correctly get its numeric value? I tried doing `(int)OrderStatus.Created`, and it seems to work, but
No answers yet.
Log in to leave an answer