當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Buffer.BlockCopy(Array, Int32, Array, Int32, Int32)用法及代碼示例


此方法用於將指定數量的字節從以特定偏移量開始的源數組複製到以特定偏移量開始的目標數組。

用法:

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 00

Array 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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。