Evgeny Lukashevich
Evgeny Lukashevich

Reputation: 1517

Unexpected operator is/as behavior when casting an int[] to object

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

Upvotes: 4

Views: 111

Answers (1)

spender
spender

Reputation: 120498

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.

Upvotes: 7

Related Questions