This is a followup to this question: Cast
enum Gender { Male, Fe
I think this is one of those cases where the C# definition of is
differs from the CLI's definition of isinst
, which evidently treats enums as their underlying base type when checking for array assignment compatibility. (Eric Lippert wrote a blog post that explains why uint[]
is treated as an int[]
by the CLI but not by C#; I suspect the same explanation applies here.) You don't even need generics to demonstrate:
Gender g = Gender.Male;
Console.WriteLine(new[] { g } is IEnumerable); // False
Console.WriteLine((object)new[] { g } is IEnumerable); // True
The first is
expression is optimized to false
at compile time because the C# compiler "knows" Gender[]
isn't an IEnumerable
. The second is
expression generates an isinst
instruction which is evaluated at run time. Quoting Eric Lippert:
It is unfortunate that C# and the CLI specifications disagree on this minor point but we are willing to live with the inconsistency.