How to cast int to enum in C#?

How to cast int to enum in C#?

In C#, you can cast an integer value to an enum using the Enum class. Here's an example:

enum Color
{
    Red,
    Green,
    Blue
}

int value = 1; // Value to cast to enum
Color color = (Color)Enum.ToObject(typeof(Color), value);

In this example, we define an enum named Color with three values: Red, Green, and Blue. We also define an integer variable value with a value of 1.

To cast value to the Color enum, we use the Enum.ToObject method and pass in the typeof(Color) to specify the target enum type, and the integer value value. We then cast the result of this operation to the Color enum using the (Color) syntax.

After the cast, the color variable will have a value of Color.Green, which corresponds to the integer value 1 in the Color enum.

Examples

  1. "C# cast int to enum using direct casting"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum)intValue;
    

    Description: Uses direct casting to convert the integer value to the enum type MyEnum.

  2. "C# convert int to enum with Enum.ToObject"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum)Enum.ToObject(typeof(MyEnum), intValue);
    

    Description: Uses Enum.ToObject to convert the integer value to the corresponding enum value.

  3. "C# cast int to enum with Enum.Parse"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), intValue.ToString());
    

    Description: Utilizes Enum.Parse to convert the integer value to the corresponding enum value.

  4. "C# cast int to enum with Enum.TryParse"

    int intValue = // ...;
    MyEnum enumValue;
    if (Enum.TryParse(intValue.ToString(), out enumValue))
    {
        // Use enumValue
    }
    

    Description: Uses Enum.TryParse for converting the integer value to the corresponding enum value with error handling.

  5. "C# convert int to enum using Enum.GetName"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum)Enum.Parse(typeof(MyEnum), Enum.GetName(typeof(MyEnum), intValue));
    

    Description: Uses Enum.GetName to get the name of the enum value and then parses it back to the enum type.

  6. "C# cast int to enum with conditional operator"

    int intValue = // ...;
    MyEnum enumValue = Enum.IsDefined(typeof(MyEnum), intValue) ? (MyEnum)intValue : DefaultValue;
    

    Description: Checks if the integer value is defined in the enum and performs the cast or uses a default value.

  7. "C# convert int to enum with type casting and is operator"

    int intValue = // ...;
    MyEnum enumValue = (Enum.IsDefined(typeof(MyEnum), intValue) ? (MyEnum?)intValue : null).GetValueOrDefault();
    

    Description: Checks if the integer value is defined in the enum and performs the cast or uses a default value.

  8. "C# cast int to enum using type casting and null coalescing"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum?)intValue ?? DefaultValue;
    

    Description: Uses type casting and null coalescing to handle the cast from int to enum.

  9. "C# convert int to enum with Enum.GetValues"

    int intValue = // ...;
    MyEnum enumValue = (MyEnum)Enum.GetValues(typeof(MyEnum)).GetValue(intValue);
    

    Description: Uses Enum.GetValues to obtain an array of enum values and then retrieves the value at the specified index.

  10. "C# cast int to enum with generic method"

    int intValue = // ...;
    MyEnum enumValue = ConvertIntToEnum<MyEnum>(intValue);
    
    // Generic method
    public TEnum ConvertIntToEnum<TEnum>(int value) where TEnum : struct
    {
        return (TEnum)Enum.ToObject(typeof(TEnum), value);
    }
    

    Description: Utilizes a generic method for casting and converting int to the specified enum type.


More Tags

actionlink ngfor production-environment http-get android-virtual-device wowza dynamic-rdlc-generation database-performance angular2-forms angular2-custom-pipes

More C# Questions

More Stoichiometry Calculators

More Weather Calculators

More Geometry Calculators

More Dog Calculators