Open In App

C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This method is used to copy a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.
Syntax:
 

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

Parameters: 
 

src: It is the source buffer.
srcOffset: It is the zero-based byte offset into src.
dst: It is the destination buffer.
dstOffset: The zero-based byte offset into dst.
count: The number of bytes to copy. 
 

Exceptions: 
 

  • ArgumentNullException: if src or dst is null.
  • ArgumentException: if src or dst is not an array of primitives OR the number of bytes in src is less than srcOffset plus count. OR the number of bytes in dst is less than dstOffset plus count.
  • ArgumentOutOfRangeException: if srcOffset, dstOffset, or count is less than 0.

Below programs illustrate the use of Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method:
Example 1:
 

CSHARP




// 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++) {
         
        // 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();
}
}


Output:
 

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 
 

Example 2: For ArgumentNullException 
 

CSHARP




// 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++) {
         
        // 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();
}
}


Output:
 

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 
 

Example 3: For ArgumentOutOfRangeException 
 

CSHARP




// 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++) {
         
        // 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();
}
}


Output:
 

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 
 

Example 4: For ArgumentException
 

CSHARP




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


Output:
 

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 
 

Reference: 
 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads