📜  C#| Buffer.BlockCopy(Array,Int32,Array,Int32,Int32)方法

📅  最后修改于: 2021-05-29 16:20:05             🧑  作者: Mango

此方法用于将指定数量的字节从以特定偏移量开始的源数组复制到以特定偏移量开始的目标数组。

句法:

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

参数:

例外情况:

  • ArgumentNullException:如果srcdst为null。
  • ArgumentException:如果srcdst不是原始数组,或者src中的字节数小于srcOffset加上countdst中的字节数小于dstOffset加上count
  • ArgumentOutOfRangeException:如果srcOffsetdstOffsetcount小于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();
}
}

输出:

示例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();
}
}

输出:

示例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();
}
}

输出:

示例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();
}
}

输出:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.buffer.blockcopy?view=netframework-4.7.2