我可以在即时窗口中执行 var b = *myObject;
,它会给我一个 0x123456
格式的值。但我不能在代码中做到这一点。然后它说
The * or -> operator must be applied to a pointer
为什么我可以在即时窗口中执行此操作,但不能在代码中执行?
最佳答案
使用不安全指针访问的示例 vs.编码
IntPtr pLineBuf;
int[] lineData;
//.
//.
//.
pLineBuf = Marshal.AllocHGlobal(8192 * sizeof(byte));
lineData = new int[8192];
// access elements like this
unsafe
{
byte* pa = (byte*)plineBuf; // cast from IntPtr
for (int j=0; j<10; j++)
lineData[j] = *(pa + j);
}
// OR
for (int j=0; j<10; j++)
lineData[j] = (Marshal.ReadByte(plineBuf + j));
关于c# - * 运算符必须应用于代码中的指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32519830/