Dailycode.info

Short solution for short problems

Int value to enum

Sometimes I post just very easy and simple things. This is one of them, but often I forget this and start looking for it on the web.

When you create an enum, its easy to extract the int value of the objects. But how can we find the corresponding object when we have the int value?

Enum MyEnum
{
Mark=0,
Jetro=1
}

Simple:

MyEnum enumValue = (MyEnum)0; //(this returns Mark)
MyEnum enumValue = (MyEnum)1; //(this returns Jetro)

You can cast an enum easely to int:

int intValue = (int) MyEnum.Mark;
 

Have a good day!