What is being asked here has not been entirely clear to me but I hope I got it now.
Given the code below:
void F(Object obj) {
var isEnum obj is Enum;
...
}
What parts of the C# standard specifies that isEnum
is true when obj
is an instance of an enum type?
In 14.9.10 is operator in the C# Language Specification there are five bullets describing how it is evaluated:
The 1st bullet is about cases where obj
has a more specific type than System.Object
.
The 2nd bullet is about nullable types.
The 4th bullet is about generic types.
The 5th bullet is when there are no matches and the is
operator evaluates to false which we know it doesn't.
You would expect that the 3rd bullet applies to the code above. The 3rd bullet has four sub-bullets:
The 1st sub-bullet applies when obj
is null.
The 2nd sub-bullet is about nullable types.
The 4th sub-bullet is when there are no matches and the is
operator evaluates to false which we know it doesn't.
You would expect the the 3rd sub-bullet applies:
Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if
R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type
and T is an interface type that is implemented by R, the result is true.
However, there seems to be something specific about enum types missing here. Assuming obj
is an instance of enum type MyEnum
none of the clauses matches the code above:
R and T are not the same type because R is MyEnum
and T is System.Enum
.
R is MyEnum
which is a value type (11.1.9) and not a reference type.
T is System.Enum
which is not an interface type.
I don't want to claim that there is an error in the specification, but after a detailed reading of 14.9.10 I'm unable to see how is Enum
can evaluate to true given a boxed reference to an enum type.
Knowing that standards people in general are much smarter than I am I probably overlooked something, but even if I didn't this shouldn't stop you from using is Enum
to test if a type is an enum. I'm sure that it is not an implementation detail that it can used like this.