c#也可以直接操作內存指針,如下為示例:
unsafe
{byte[] a = {1,2,3};fixed (byte* p1 = a, p2 = &a[^1]){Debugger.Log(1, "test", $"max index:{p2-p1}");Debugger.Log(1, "test", $"address:{(long)p1:X}");Debugger.Log(1, "test", $"第一個元素的值:{*p1}");}
}
p1為a的地址,p2為結尾地址。
注意,這是unsafe的。
參數網址:
fixed statement - pin a moveable variable - C# reference | Microsoft Learn
從這個網址進去,可以進入到c#的內存指針操作的很多內容