Friday, May 25, 2012

Cast int to Enum in C#


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



Source: Tips4all

10 comments:

  1. 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);

    ReplyDelete
  2. Just cast it:

    MyEnum e = (MyEnum)3;


    You can check if it's in range using Enum.IsDefined:

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

    ReplyDelete
  3. int one = 1;

    MyEnum e = (MyEnum)one;

    ReplyDelete
  4. For those people stopping by, who wants to use as an extension method.

    public static T ToEnum<T>(this string enumString)
    {
    return (T) Enum.Parse(typeof (T), enumString);
    }


    Usage

    Color colorEnum = "Red".ToEnum<Color>();


    OR

    string color = "Red";
    var colorEnum = color.ToEnum<Color>();

    ReplyDelete
  5. Below is a nice utility class for Enums

    public static class EnumHelper
    {
    public static int[] ToIntArray<T>(T[] value)
    {
    int[] result = new int[value.Length];
    for (int i = 0; i < value.Length; i++)
    result[i] = Convert.ToInt32(value[i]);
    return result;
    }

    public static T[] FromIntArray<T>(int[] value)
    {
    T[] result = new T[value.Length];
    for (int i = 0; i < value.Length; i++)
    result[i] = (T)Enum.ToObject(typeof(T),value[i]);
    return result;
    }


    internal static T Parse<T>(string value, T defaultValue)
    {
    if (Enum.IsDefined(typeof(T), value))
    return (T) Enum.Parse(typeof (T), value);

    int num;
    if(int.TryParse(value,out num))
    {
    if (Enum.IsDefined(typeof(T), num))
    return (T)Enum.ToObject(typeof(T), num);
    }

    return defaultValue;
    }
    }

    ReplyDelete
  6. Sometimes you have and object to the MyEnum type. Like

    var MyEnumType = typeof(MyEnumType);


    then:

    Enum.ToObject(typeof(MyEnum), 3)

    ReplyDelete
  7. If you have an integer that acts as a bitmask and could represent one or more values in a [Flags] enumeration, you can use this code to parse the individual flag values into a list:

    for (var flagIterator = 0x1; flagIterator <= 0x80000000; flagIterator <<= 1)
    {
    // Check to see if the current flag exists in the bit mask
    if ((intValue & flagIterator) != 0)
    {
    // If the current flag exists in the enumeration, then we can add that value to the list
    // if the enumeration has that flag defined
    if (Enum.IsDefined(typeof(MyEnum), flagIterator))
    ListOfEnumValues.Add((MyEnum)flagIterator);
    }
    }

    ReplyDelete
  8. I am using this piece of code to cast int to my enum:

    if (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
    else { //handle it here, if its not defined }


    I find it the best solution.

    ReplyDelete
  9. If you're ready for the 4.0 .Net Framework, there's a new Enum.TryParse() function that's very useful and plays well with the [Flags] attribute. See http://msdn.microsoft.com/en-us/library/dd783499.aspx

    ReplyDelete
  10. You can use TryParse():

    Color mycolor;
    if(Enum.TryParse<Color>("Red", out mycolor)){
    //Parse Color Successfully
    }

    ReplyDelete