Let’s have a byte array
byte[] Test = { 0, 0, 0, 0 };
I want to do this:
UInt32* p = &Test[0];
p* = 0xAABBCCDD;
or shortly:
(int*)(&Test[0])* = 0xAABBCCDD;
In Delphi I’d do:
PUInt32(@Test[0])^ := $AABBCCDD;
where PUInt32 = ^Uint32 or typdef PUInt32 = *UInt32 in C#.
But don’t know how it works in C#.
This kind of code is fundamentally unsafe, it defeats the compiler’s type system. And won’t be accepted by the verifier. If you accidentally declare the array wrong, 3 elements for example, then you’ll corrupt memory and produce a very hard to diagnose bug.
This can however also be done safely:
Which the slight disadvantage that it creates the array for you. Bypassing that requires the unsafe version:
Which requires you to use the unsafe keyword in the method declaration and tick the Project + Properties, Build, “Allow unsafe code” option.