In unsafe code in C#, I assigned a pointer to the managed variable of an array type:
int[] array = new int[3];
// ...
fixed (int* ptr = array)
{
// Some code
}
Then I looked at corresponding part of the IL code:
.locals init ([0] int32[] 'array',
[1] int32& pinned ptr)
Since this is unsafe code, and int* ptr is declaration of unmanaged pointer (or I think so at the moment), why in the CIL code doesn’t write int32* ptr, instead of int32& ptr?
http://www.ecma-international.org/publications/standards/Ecma-335.htm
Page 334
Page 149
I agree with Hans as to the rational behind the msil language design choice.
These two things are different:
vs.
If you look at the IL created for the second one, you’ll see this (which I think is what you’re expecting):
In the first version (your version), you’re pointing to an instance of System.Array (a managed type). In my version (using stackalloc) you’re pointing to what I think you’re expecting to point to… a block of memory large enough for 5 ints.