lomaxx lomaxx - 6 months ago 62
C# Question

Cast int to enum in C#

How can an

int
be cast to an
enum
in C#?

Answer

From a string:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

From an int:

YourEnum foo = (YourEnum)yourInt;

Update:

From number you can also

YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum) , yourInt);