// fastcopy.cs
// 编译时使用:/unsafe
using System;
class Test
{
// unsafe 关键字允许在下列
// 方法中使用指针:
static unsafe void Copy(byte[] src, int srcIndex, byte[] dst, int dstIndex, int count)
{
if (src == null || srcIndex < 0 ||
dst == null || dstIndex < 0 || count < 0)
{
throw new ArgumentException();
}
int srcLen = src.Length;
int dstLen = dst.Length;
if (srcLen - srcIndex < count || dstLen - dstIndex < count)
{
throw new ArgumentException();
}
// 以下固定语句固定
// src 对象和 dst 对象在内存中的位置,以使这两个对象
// 不会被垃圾回收移动。
fixed (byte* pSrc = src, pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;
// 以 4 个字节的块为单位循环计数,一次复制