4

Could someone please explain why this is happening?

var y = new int[]{1,2};

Console.WriteLine(y is uint[]); // false
Console.WriteLine(((object)y) is uint[]); // true
Evgeny Lukashevich
  • 1,500
  • 14
  • 26
  • 1
    Kinda dupe: http://stackoverflow.com/questions/593730/why-does-int-is-uint-true-in-c-sharp – Rawling Jun 15 '12 at 09:51
  • I believe the first should generate a compiler warning [Indeed it does *expression is never of the provided uint[] type*) – V4Vendetta Jun 15 '12 at 09:52

1 Answers1

7

In c# you can't cast an int to a uint, so the first test fails because it is compiled to constant false.

However, int->uint cast is allowed by the CLR. The second check cannot be deduced by the compiler and therefore must be calculated at runtime. As you've dodged compiler checks, the CLR allows it.

spender
  • 117,338
  • 33
  • 229
  • 351