Why do castings between arrays of some types of values ​​seem to be a security breach?

The following code:

object array = new int[] {-1};
Console.WriteLine("array is uint[]: {0}", array is uint[]);
Console.WriteLine("array[0]: {0:X}", ((uint[])array)[0]);

Displays the following errors without errors:

array is uint []: True
array [0]: FFFFFFFF

This seems special to me because it seems to violate type safety. It is also a compile time error to do the following:

int[] array = {-1};
uint[] test = (uint[])array;

Where does this inconsistency come from? Why is the CLR implemented as follows?

: , , . , ; , .
( ). , test[0] Int32.

+4
1

.NET .

# , .NET. , :

uint[] test = (uint[])new int[10]; // Compiler Error CS0030 C#
                                   // doesn't allow this covariance

#;.NET , , ( ) .

, , .NET, # object, , . , uint[] test = (uint[])(object)new int[10]; , :

object temp = new int[10];  // normal enough asignmnt.
uint[] test = (uint[])temp; // C# doesn't allow assigning
                            // int[] to uint[] but temp is
                            // object so the compiler
                            // doesn't know that what
                            // you are doing, and .NET
                            // does allow it.

:

, CLR ? CLI (.. )

, CIL , signed unsigned. clt, , , , clt.un , , .

, CIL.

, , , , , ; ( - ). # int uint ; , .

+2

All Articles