如何使用指针复制字节数组(C# 编程指南)How to use pointers to copy an array of bytes (C# Programming Guide)
04/20/2018
本文内容
下面的示例使用指针将字节从一个数组复制到另一个数组。The following example uses pointers to copy bytes from one array to another.
此示例使用 unsafe 关键字,使你可以在 Copy 方法中使用指针。This example uses the unsafe keyword, which enables you to use pointers in the Copy method. fixed 语句用于声明指向源数组和目标数组的指针。The fixed statement is used to declare pointers to the source and destination arrays. fixed 语句将源数组和目标数组的位置固定在内存中,以便它们不会被垃圾回收所移动。The fixed statement pins the location of the source and destination arrays in memory so that they will not be moved by garbage collection. 当完成 fixed 块后,将取消固定数组的内存块。The memory blocks for the arrays are unpinned when the fixed block is completed. 因为此示例中的 Copy 方法使用 unsafe 关键字,所以必须使用 -unsafe 编译器选项对其进行编译。Because the Copy method in this example uses the unsafe keyword, it must be compiled with the -unsafe compile