Consider the following code:
I build it with .NET framework 4.7.2
.
ushort[] a = new ushort[100];
if (a is short[])
{
Assert.Fail("This never happens.");
}
object b = a;
if (b is short[])
{
Assert.Fail("Why?");
}
The first test works as expected (a
is not of short[]
). My question concerns the second test, which, unexpectedly, does not fail.
Why is it so?
EDIT: added MSIL (Release, optimized)
.maxstack 8
IL_0000: ldc.i4.s 100
IL_0002: newarr [mscorlib]System.UInt16
IL_0007: isinst int16[]
IL_000c: brfalse.s IL_0018
IL_000e: ldstr "Why\?"
IL_0013: call void [(...).TestFramework](...).Assert::Fail(string)
IL_0018: ret