此方法用于将指定数量的字节从以特定偏移量开始的源数组复制到以特定偏移量开始的目标数组。
用法:
public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count);
参数:
- src:它是源缓冲区。
- srcOffset:它是src中从零开始的字节偏移量。
- dst:它是目标缓冲区。
- dstOffset:从零开始的字节偏移为dst。
- count:要复制的字节数。
异常:
- ArgumentNullException:如果src或dst为null。
- ArgumentException:如果src或dst不是原始数组,或者src中的字节数小于srcOffset加上count。或dst中的字节数小于dstOffset加上count。
- ArgumentOutOfRangeException:如果srcOffset,dstOffset或count小于0。
以下示例程序旨在说明Buffer.BlockCopy(Array,Int32,Array,Int32,Int32)方法的使用:
范例1:
// C# program to demonstrate
// Buffer.BlockCopy(Array, Int32,
// Array, Int32, Int32) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] src = {15, 16, 17, 18 };
long[] dest = {17, 18, 19, 20 };
Console.WriteLine("Initial Array values:");
Console.WriteLine();
// Display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display Individual byte
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
// copying the specified number of bytes
// using Buffer.BlockCopy() method
Buffer.BlockCopy(src, 4, dest, 7, 6);
Console.WriteLine();
Console.WriteLine("Array after operation:");
Console.WriteLine();
// display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display hexadecimal value
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Display the individual
// array element values
// in hexadecimal.
public static void displayhexvalue(Array a,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// Display value one by one
for (int i = 0; i < a.Length; i++)
Console.Write(" {0:X16} ", a.GetValue(i));
Console.WriteLine();
}
// Display the individual
// bytes in the array
// in hexadecimal.
public static void displaybytes(Array arr,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// loop to traverse
// every element of the array
for (int i = 0; i < arr.Length; i++) {
// gettig byte array
// converted from long array
byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i));
// display each byte value
foreach(byte byteValue in bytes)
Console.Write(" {0:X2}", byteValue);
}
Console.WriteLine();
}
}
输出:
Initial Array values:
Array element in hexadecimal form:
src:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest:0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src:0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
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 00Array after operation:
Array element in hexadecimal form:
src:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest:0000000000000011 0000000010000000 0000000000000013 0000000000000014
Individual bytes:
src:0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest:11 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
范例2:对于ArgumentNullException
// C# program to demonstrate
// Buffer.BlockCopy(Array, Int32,
// Array, Int32, Int32) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] src = {15, 16, 17, 18};
long[] dest = {17, 18, 19, 20};
Console.WriteLine("Initial Array values:");
Console.WriteLine();
// Display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display Individual byte
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
// copying the specified number of bytes
// using Buffer.BlockCopy() method
Console.WriteLine("src and dst are null:");
Buffer.BlockCopy(null, 4, null, 7, 6);
Console.WriteLine();
Console.WriteLine("Array after operation:");
Console.WriteLine();
// display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display hexadecimal value
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Display the individual array
// element values in hexadecimal.
public static void displayhexvalue(Array a,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// Display value one by one
for (int i = 0; i < a.Length; i++)
Console.Write(" {0:X16} ", a.GetValue(i));
Console.WriteLine();
}
// Display the individual bytes
// in the array in hexadecimal.
public static void displaybytes(Array arr,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// loop to traverse every
// element of the array
for (int i = 0; i < arr.Length; i++) {
// gettig byte array converted
// from long array
byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i));
// display each byte value
foreach(byte byteValue in bytes)
Console.Write(" {0:X2}", byteValue);
}
Console.WriteLine();
}
}
输出:
Initial Array values:
Array element in hexadecimal form:
src:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest:0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src:0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
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
src and dst are null:
Exception Thrown:System.ArgumentNullException
范例3:对于ArgumentOutOfRangeException
// C# program to demonstrate
// Buffer.BlockCopy(Array, Int32,
// Array, Int32, Int32) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] src = {15, 16, 17, 18};
long[] dest = {17, 18, 19, 20};
Console.WriteLine("Initial Array values:");
Console.WriteLine();
// Display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display Individual byte
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
// copying the specified number of bytes
// using Buffer.BlockCopy() method
Console.WriteLine("srcoffest and destoffset are less than zero:");
Buffer.BlockCopy(src, -4, dest, -7, 6);
Console.WriteLine();
Console.WriteLine("Array after operation:");
Console.WriteLine();
// display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display hexadecimal value
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Display the individual array
// element values in hexadecimal.
public static void displayhexvalue(Array a,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// Display value one by one
for (int i = 0; i < a.Length; i++)
Console.Write(" {0:X16} ", a.GetValue(i));
Console.WriteLine();
}
// Display the individual bytes
// in the array in hexadecimal.
public static void displaybytes(Array arr,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// loop to traverse every
// element of the array
for (int i = 0; i < arr.Length; i++) {
// gettig byte array converted
// from long array
byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i));
// display each byte value
foreach(byte byteValue in bytes)
Console.Write(" {0:X2}", byteValue);
}
Console.WriteLine();
}
}
输出:
Initial Array values:
Array element in hexadecimal form:
src:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest:0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src:0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
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
srcoffest and destoffset are less than zero:
Exception Thrown:System.ArgumentOutOfRangeException
范例4:对于ArgumentException
// C# program to demonstrate
// Buffer.BlockCopy(Array, Int32,
// Array, Int32, Int32)
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] src = {15, 16, 17, 18};
long[] dest = {17, 18, 19, 20};
Console.WriteLine("Initial Array values:");
Console.WriteLine();
// Display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display Individual byte
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
// copying the specified number of bytes
// using Buffer.BlockCopy() method
Console.WriteLine("srcoffest and destoffset are less than zero:");
Buffer.BlockCopy(src, 4, dest, 7, 70);
Console.WriteLine();
Console.WriteLine("Array after operation:");
Console.WriteLine();
// display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(src, "src");
displayhexvalue(dest, "dest");
// display hexadecimal value
Console.WriteLine("Individual bytes:");
displaybytes(src, "src");
displaybytes(dest, "dest");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Display the individual array
// element values in hexadecimal.
public static void displayhexvalue(Array a,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// Display value one by one
for (int i = 0; i < a.Length; i++)
Console.Write(" {0:X16} ", a.GetValue(i));
Console.WriteLine();
}
// Display the individual bytes
// in the array in hexadecimal.
public static void displaybytes(Array arr,
string name)
{
// print the name
Console.Write("{0, 5}:", name);
// loop to traverse every
// element of the array
for (int i = 0; i < arr.Length; i++) {
// getting byte array
// converted from long array
byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i));
// display each byte value
foreach(byte byteValue in bytes)
Console.Write(" {0:X2}", byteValue);
}
Console.WriteLine();
}
}
输出:
Initial Array values:
Array element in hexadecimal form:
src:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest:0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src:0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
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
srcoffest and destoffset are less than zero:
Exception Thrown:System.ArgumentException
参考:
相关用法
- C# Array.BinarySearch(Array, Int32, Int32, Object)用法及代码示例
- C# Array.BinarySearch(Array, Int32, Int32, Object, IComparer)用法及代码示例
- C# Int32.GetTypeCode用法及代码示例
- C# Char.ConvertFromUtf32(Int32)用法及代码示例
- C# Int32.GetHashCode用法及代码示例
- C# Int32.CompareTo用法及代码示例
- C# Int32.Equals用法及代码示例
- C# Int32.Parse(String)用法及代码示例
- C# Char.IsSurrogatePair(String, Int32)用法及代码示例
- C# Char.IsControl(String, Int32)用法及代码示例
- C# Char.IsHighSurrogate(String, Int32)用法及代码示例
- C# Char.ConvertToUtf32(String, Int32)用法及代码示例
- C# Char.IsSurrogate(String, Int32)用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。