This is a followup to this question: Cast<int>.Cast<int?> applied on generic enum collection results in invalid cast exception
enum Gender { Male, Female }
Gender g = Gender.Male;
bool b = g is int; // false, alright no issues
b = new[] { g } is IEnumerable<int>; // false, alright no issues
b = Is<Gender, int>(g); //false, alright no issues
b = Is<Gender[], IEnumerable<int>>(new[] { g }); // true, why on earth !!!
static bool Is<S, T>(S s)
{
return s is T;
}
Why is that Gender[] is IEnumerable<int>
returns true
in the generic case? Especially when they are not type compatible?
IEnumerable<int> c = new[] { Gender.Male }; //not compilable
It had tripped me in the question I linked! I think this question is the crux of the issue of the linked question.
For someone interested, this is a corner case with arrays (not really enums). Follow Eric Lippert's blog article in the answer to know more of this edge case. This doesn't happen with List<T>
for instance:
b = Is<List<Gender>, IEnumerable<int>>(new List<Gender> { g }); // false, rightly
I think this is one of those cases where the C# definition of
is
differs from the CLI's definition ofisinst
, which evidently treats enums as their underlying base type when checking for array assignment compatibility. (Eric Lippert wrote a blog post that explains whyuint[]
is treated as anint[]
by the CLI but not by C#; I suspect the same explanation applies here.) You don't even need generics to demonstrate:The first
is
expression is optimized tofalse
at compile time because the C# compiler "knows"Gender[]
isn't anIEnumerable<int>
. The secondis
expression generates anisinst
instruction which is evaluated at run time. Quoting Eric Lippert: