首页 > 解决方案 > 运算符是否无法区分 short[] 和 ushort[]?

问题描述

考虑以下代码:

我用.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?");
}

第一个测试按预期工作(a不是short[])。我的问题涉及第二个测试,出乎意料的是,它没有失败。

为什么会这样?

编辑:添加 MSIL(发布,优化)

 .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

标签: c#.netshortushort

解决方案


推荐阅读