ulong[] to uint[] is this good practice?

I stumpled upon this problem and saw this solution:

ulong[] ulongArray = { 1, 2, 3, 4 };
uint[]  uintArray  = null;

uintArray  = (uint[]) (object) ulongArray;
ulongArray = (ulong[])(object) uintArray;

and I am wondering if that is how you do it? Looks a little strange to me ..

Jon Skeet
people
quotationmark

While that will compile, it will throw an exception at execution time. The two types just aren't compatible.

A similar situation would be:

uint[] uintArray = { 1, 2, 3, 4 };
int[] intArray = (int[]) (object) uintArray;

That will work - the CLR is happy to treat a uint[] reference as an int[] reference, even though C# isn't. (The same is true for conversions between arrays of enum types and their underlying integral types.)

I'd be wary of using it, just in terms of surprising anyone reading the code - but in certain cases it could be appropriate.

people

See more on this question at Stackoverflow