How to convert int to enum in C#? Type casting and Enum.Parse
Hello everyone! I'm a beginner C# developer and I've encountered a situation I don't understand. I have an enum for order statuses:
```csharp
public enum OrderStatus
{
Created = 1,
Processing = 2,
Shipped = 3,
Delivered = 4
}
```
From the database, I get a numeric status value as an int (for example, 2). I need to convert this int to an OrderStatus enum member. I tried simply assigning:
```csharp
int statusValue = 2;
OrderStatus status = statusValue; // Compilation error!
```
But I get an error: "Cannot implicitly convert type 'int' to 'OrderStatus'". I understand that C# is a strongly typed language, but how do I correctly perform such a conversion?
I've heard about explicit casting and the Enum.Parse method, but I'm not entirely
No answers yet.
Log in to leave an answer