This is false: typeof(double).IsAssignableFrom(typeof(int))
This is false: typeof(int).IsAssignableFrom(typeof(double))
But this works:
double a = 1.0;
int b = 1;
a = b;
Clearly a double is assignable from an int but the framework IsAssignableFrom() gets it wrong.
Why? Or is this a bug in .NET caused by the special nature of int and double which have no inheritance relationship but are assignable (in one direction)?
C# is providing the implicit conversion from
inttodouble. That’s a language decision, not something which .NET will do for you… so from the .NET point of view,doubleisn’t assignable fromint.(As an example of why this is language-specific, F# doesn’t perform implicit conversions for you like this – you’d need to explicitly specify the conversion.)
It’s worth looking at the documentation for
Type.IsAssignableFrom(edited very slightly for readability):Now apply that to
doubleandintand you’ll see it should return false.