System.Buffer.BlockCopy 方法

方法描述

将指定数目的字节从起始于特定偏移量的源数组复制到起始于特定偏移量的目标数组。

语法定义(C# System.Buffer.BlockCopy 方法 的用法)

public static void BlockCopy(
	Array src,
	int srcOffset,
	Array dst,
	int dstOffset,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
src System-Array 源缓冲区。
srcOffset System-Int32 src 中从零开始的字节偏移量。
dst System-Array 目标缓冲区。
dstOffset System-Int32 dst 中从零开始的字节偏移量。
count System-Int32 要复制的字节数。
返回值 void

提示和注释

此方法将 count 个字节从 src 复制到 dst,前者开始于 srcOffset 而后者开始于 dstOffset。 srcOffset 和 dstOffset 都是从零开始的;也就是说,每个缓冲区中的第一个字节都在位置 0,而不是位置 1。

BlockCopy 方法使用内存的偏移量访问 src 参数数组中的字节,而不是使用索引或数组上下限等编程构造。 例如,如果使用应用程序的编程语言声明一个从零开始的下限为 -50 的 Int32 数组,然后将该数组和偏移量 5 传递给 BlockCopy 方法,则该方法将访问的第一个数组元素是该数组的第二个元素(位于索引 -49 处)。 此外,首先访问数组元素 -49 的哪个字节取决于执行应用程序的计算机的 Endian 设置。

顾名思义,BlockCopy 方法会将字节块作为一个整体复制,而不是一次复制一个字节。 因此,如果 src 和 dst 引用相同数组,并且 srcOffset + count - 1 的范围与 dstOffset + count -1 的范围重叠,则在将重叠字节的值复制到目标之前不会覆盖这些值。 在下面的示例中,名为 arr 的数组中字节 0-16 的值会复制到字节 12-28。 尽管有重叠范围,还是成功复制了源字节的值。

C#

VB

复制

const int INT_SIZE = 4;

int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

Buffer.BlockCopy(arr, 0 * INT_SIZE, arr, 3 * INT_SIZE, 4 * INT_SIZE);

foreach (int value in arr)

Console.Write("{0} ", value);

// The example displays the following output:

// 2 4 6 2 4 6 8 16 18 20

在下面的示例中,名为 arr 的数组中字节 12-28 的值会复制到字节 0-16。 再一次,尽管有重叠范围,还是成功复制了源字节的值。

C#

VB

复制

const int INT_SIZE = 4;

int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

Buffer.BlockCopy(arr, 3 * INT_SIZE, arr, 0 * INT_SIZE, 4 * INT_SIZE);

foreach (int value in arr)

Console.Write("{0} ", value);

// The example displays the following output:

// 8 10 12 14 10 12 14 16 18 20

System.Buffer.BlockCopy 方法例子

该示例阐释了在使用 BlockCopy 方法时考虑系统的 endianness 的重要性:因为 Windows 系统为小 endian,因此,基元数据类型的值的低序位字节位于高序位字节之前。

using System;

class Example
{
    // Display the individual bytes in the array in hexadecimal.
    public static void DisplayArray(Array arr, string name)
    {
        Console.WindowWidth = 120;
        Console.Write("{0,11}:", name);
        for (int ctr = 0; ctr < arr.Length; ctr++)
        {
            byte[] bytes;
            if (arr is long[])
               bytes = BitConverter.GetBytes((long) arr.GetValue(ctr));
            else
               bytes = BitConverter.GetBytes((short) arr.GetValue(ctr));

            foreach (byte byteValue in bytes)
               Console.Write(" {0:X2}", byteValue);
        }
        Console.WriteLine();
    }

    // Display the individual array element values in hexadecimal.
    public static void DisplayArrayValues(Array arr, string name)
    {
        // Get the length of one element in the array.
        int elementLength = Buffer.ByteLength(arr) / arr.Length;
        string formatString = String.Format(" {{0:X{0}}}", 2 * elementLength);
        Console.Write( "{0,11}:", name);
        for (int ctr = 0; ctr < arr.Length; ctr++)
            Console.Write(formatString, arr.GetValue(ctr));

        Console.WriteLine();
    }

    public static void Main( )
    {
        // These are the source and destination arrays for BlockCopy.
        short[] src  = { 258, 259, 260, 261, 262, 263, 264, 
                          265, 266, 267, 268, 269, 270 };
        long[] dest = { 17, 18, 19, 20 };

        // Display the initial value of the arrays in memory.
        Console.WriteLine( "Initial values of arrays:");
        Console.WriteLine("   Array values as Bytes:");
        DisplayArray(src, "src" );
        DisplayArray(dest, "dest");
        Console.WriteLine("   Array values:");
        DisplayArrayValues(src, "src");
        DisplayArrayValues(dest, "dest");
        Console.WriteLine();

        // Copy bytes 5-10 from source to index 7 in destination and display the result.
        Buffer.BlockCopy(src, 5, dest, 7, 6);
        Console.WriteLine("Buffer.BlockCopy(src, 5, dest, 7, 6 )");
        Console.WriteLine("   Array values as Bytes:");
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");
        Console.WriteLine("   Array values:");
        DisplayArrayValues(src, "src");
        DisplayArrayValues(dest, "dest");
        Console.WriteLine();

        // Copy bytes 16-20 from source to index 22 in destination and display the result. 
        Buffer.BlockCopy(src, 16, dest, 22, 5);
        Console.WriteLine("Buffer.BlockCopy(src, 16, dest, 22, 5)");
        Console.WriteLine("   Array values as Bytes:");
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");
        Console.WriteLine("   Array values:");
        DisplayArrayValues(src, "src");
        DisplayArrayValues(dest, "dest");
        Console.WriteLine();

        // Copy overlapping range of bytes 4-10 to index 5 in source.
        Buffer.BlockCopy(src, 4, src, 5, 7 );
        Console.WriteLine("Buffer.BlockCopy( src, 4, src, 5, 7)");
        Console.WriteLine("   Array values as Bytes:");
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");
        Console.WriteLine("   Array values:");
        DisplayArrayValues(src, "src");
        DisplayArrayValues(dest, "dest");
        Console.WriteLine();

        // Copy overlapping range of bytes 16-22 to index 15 in source. 
        Buffer.BlockCopy(src, 16, src, 15, 7);
        Console.WriteLine("Buffer.BlockCopy( src, 16, src, 15, 7)");
        Console.WriteLine("   Array values as Bytes:");
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");
        Console.WriteLine("   Array values:");
        DisplayArrayValues(src, "src");
        DisplayArrayValues(dest, "dest");
    }
}
// The example displays the following output:
//    Initial values of arrays:
//       Array values as Bytes:
//            src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
//           dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
//       Array values:
//            src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
//           dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
//    
//    Buffer.BlockCopy(src, 5, dest, 7, 6 )
//       Array values as Bytes:
//            src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
//           dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
//       Array values:
//            src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
//           dest: 0100000000000011 0000000701060105 0000000000000013 0000000000000014
//    
//    Buffer.BlockCopy(src, 16, dest, 22, 5)
//       Array values as Bytes:
//            src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
//           dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
//       Array values:
//            src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
//           dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B
//    
//    Buffer.BlockCopy( src, 4, src, 5, 7)
//       Array values as Bytes:
//            src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
//           dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
//       Array values:
//            src: 0102 0103 0404 0501 0601 0701 0108 0109 010A 010B 010C 010D 010E
//           dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B
//    
//    Buffer.BlockCopy( src, 16, src, 15, 7)
//       Array values as Bytes:
//            src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 0A 01 0B 01 0C 01 0D 0D 01 0E 01
//           dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
//       Array values:
//            src: 0102 0103 0404 0501 0601 0701 0108 0A09 0B01 0C01 0D01 010D 010E
//           dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B

异常

异常 异常描述
ArgumentNullException src 或 dst 为 null。
ArgumentException
  • src 或 dst 不是基元数组。
  • src 的长度小于 srcOffset 加上 count。
  • dst 的长度小于 dstOffset 加上 count。
ArgumentOutOfRangeException srcOffset、dstOffset 或 count 小于 0。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。