Converting Enumerations

I love Enums – but I always have to look up how to convert them to one thing or another – so a reminder for myself:

Here’s my enumeration:

    public enum MyEnum
    {
        Alpha,
        Beta,
        Gamma
    }  

And the conversions (each enumeration item has a value (e.g. 1) and a string (e.g. “Beta”):

MyEnum someEnum = MyEnum.Beta;

//Convert to String
string someEnumString = someEnum.ToString();

//Convert to MyEnum again
MyEnum someEnum2 = (MyEnum)Enum.Parse(typeof(MyEnum), someEnumString);

//Convert to int value
int someEnumInt = (int)someEnum;

//Convert int to My Enum again
MyEnum someEnum3 = (MyEnum)someEnumInt;
Advertisements
Converting Enumerations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s