- A nested base type may be different for an enumeration, but an enumerated type cannot be added to a NULL type.
And similarly,
- An insert with a null enum value can be dropped to the base type, but the type embedded in the kernel cannot be distinguished to a null enumeration.
Well, I know that “type with a zero number in a box” is not the best way to describe it, but it is in the name of the question. I know that this is the basic type of value that appears in the box.
I will show it with examples. Suppose I have an enum with int as the base type.
enum Sex { Male, Female }
Case I:
int? i = 1; object o = i; Sex e = (Sex)o;
Case II:
Sex? e = Sex.Male; object o = e; int i = (int)o;
In a nutshell
(enum)int? -> succeeds (int?)enum -> the reverse fails (int)enum? -> succeeds (enum?)int -> the reverse fails
Or in even simpler terms
discarded to non-nullable -> successfully cast to nullable -> fail
Now I know that once you enter a value type, it can only be dropped to the original type. But since, according to C # rules, an int can be added to an int box, and an int can be added to an enum box and from an int to an int? box int? and in the box int? before int , I was looking for a consistent understanding of other scenarios, i.e. the ones listed above. But I do not understand the logic. For one, I feel that if they all failed or they all succeeded, it made sense to the developers. Two, even successful throws, look a little strange. I mean, since the type of a value can be implicitly passed into its equivalent with a null value (and not vice versa), casting to a nullable value should succeed in any case, but with the current implementation, a type with a null value is successfully transferred to a value, not nullable, which can even if the first is null. If it were all the other way around, it would be easier to understand. Example:
Sex? e = null; object o = e; int i = (int)o;
Questions:
enums casting c # nullable boxing
nawfal
source share