Why does “int[] is uint[] == true” in C#

前端 未结 5 1131
死守一世寂寞
死守一世寂寞 2020-11-27 05:12

Can somebody clarify the C# is keyword please. In particular these 2 questions:

Q1) line 5; Why does this return true?

Q2) line 7; Why no cast e

5条回答
  •  隐瞒了意图╮
    2020-11-27 05:44

    Now that's interesting. I found this in the ECMA-335 standard. 4.3 castclass. Note that:

    • Arrays inherit from System.Array.

    • If Foo can be cast to Bar, then Foo[] can be cast to Bar[].

    • For the purposes of note 2 above, enums are treated as their underlying type: thus E1[] can be cast to E2[] if E1 and E2 share an underlying type.

    You can cast int to uint, but that it behaves like this is very strange. Visual Studio does not recognize any of this, even the watch, when the debugger is attached just shows a question mark '?'.

    You might wanna take a look at this, fast forward about 10 minutes in and listen to Anders explain the co-variant array implementation. I think that is the fundamentally underlying issue here.

提交回复
热议问题