ProAnswers.org

What's easy way to cast an int to an enum in c#?

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 = Enum.ToObject(typeof(YourEnum) , yourInt);

Just cast it:

MyEnum e = (MyEnum)3;

You can check if it's in range using [Enum.IsDefined](http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx):

if (Enum.IsDefined(typeof(MyEnum), 3)) { … }