Why is enum cast on short possible but not boxed generic enum cast?

Florian Koch

I just encountered a weird restriction while porting explicitly typed code in a converter to generic code:

When having a short (Int16) it is possible to cast it to an enum type. When doing the same with a generic enum type and boxed cast ((T)(object)value) this is an invalid conversion.

I was able to make the conversion successful by adding a third cast. It now looks like this:

Int16 numericValue;
...
var enumValue = (TEnum)(Object)(Int32)numericValue;

Why is that? The following (old) code worked just fine:

Int16 numericValue;
...
var enumValue = (MyEnum)numericValue;    
Jotrius

This is a problem of boxing and unboxing. When you unboxing the object, you can only unbox to the type of the value that was originally boxed: https://msdn.microsoft.com/de-de/library/yz2be5wk.aspx

In your case, you box an Int16 to an object:

Int16 numericValue;
...
var boxedValue = (object)numericValue;

and then you try unbox it as an Int32 (enum is Int32) and this is not possible:

var enumValue = (TEnum)boxedValue; // -> System.InvalidCastException

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

From Dev

C# generic enum cast to specific enum

From Dev

EnumsNET generic method to cast enum to list

From Dev

Why is it possible to cast generic class?

From Dev

Method that accepts a "generic" Enum as a parameter, then cast to a diff enum type at runtime

From Dev

Cast<int>.Cast<int?> applied on generic enum collection results in invalid cast exception

From Dev

Cast int to enum in javascript

From Dev

How to Cast Enum To Int

From Dev

Cast Int Array to Enum Flags

From Java

Swift - Cast Int into enum:Int

From Dev

Swift integer type cast to enum

From Dev

TypeScript: Implicit number to enum cast

From Dev

How to cast an enum ordinal as a String?

From Dev

Cast Int Array to Enum Flags

From Dev

Why can enum arrays be cast to two different IEnumerable<T> types?

From Dev

Why do I need this strange cast for enum arithmetics?

From Dev

Why do I have to cast an enum element when assigning it to a same enum variable type in C?

From Dev

Enum to int, why use type cast to int rather than Convert.ToInt32(Enum)?

From Dev

Why does this generic cast fail

From Dev

jaxb: strange class cast exception on enum list

From Dev

How to cast Enum with Description attribute to dictionary?

From Java

How can I cast int to enum?

From Dev

Cast Enum values from dropdown list

From Dev

static_cast on integer to enum conversion

From Dev

Is there a way to down cast an enum rawValue to its type?

From Dev

How to cast Enum value as a constant c#

From Dev

How to iterate over an Enum, and cast the obj

From Dev

How to cast a String value to an Enum value by Class?

From Dev

Take integer value from database and cast it to Enum

Related Related

  1. 1

    Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

  2. 2

    C# generic enum cast to specific enum

  3. 3

    EnumsNET generic method to cast enum to list

  4. 4

    Why is it possible to cast generic class?

  5. 5

    Method that accepts a "generic" Enum as a parameter, then cast to a diff enum type at runtime

  6. 6

    Cast<int>.Cast<int?> applied on generic enum collection results in invalid cast exception

  7. 7

    Cast int to enum in javascript

  8. 8

    How to Cast Enum To Int

  9. 9

    Cast Int Array to Enum Flags

  10. 10

    Swift - Cast Int into enum:Int

  11. 11

    Swift integer type cast to enum

  12. 12

    TypeScript: Implicit number to enum cast

  13. 13

    How to cast an enum ordinal as a String?

  14. 14

    Cast Int Array to Enum Flags

  15. 15

    Why can enum arrays be cast to two different IEnumerable<T> types?

  16. 16

    Why do I need this strange cast for enum arithmetics?

  17. 17

    Why do I have to cast an enum element when assigning it to a same enum variable type in C?

  18. 18

    Enum to int, why use type cast to int rather than Convert.ToInt32(Enum)?

  19. 19

    Why does this generic cast fail

  20. 20

    jaxb: strange class cast exception on enum list

  21. 21

    How to cast Enum with Description attribute to dictionary?

  22. 22

    How can I cast int to enum?

  23. 23

    Cast Enum values from dropdown list

  24. 24

    static_cast on integer to enum conversion

  25. 25

    Is there a way to down cast an enum rawValue to its type?

  26. 26

    How to cast Enum value as a constant c#

  27. 27

    How to iterate over an Enum, and cast the obj

  28. 28

    How to cast a String value to an Enum value by Class?

  29. 29

    Take integer value from database and cast it to Enum

HotTag

Archive