C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
Last Updated :
02 Jul, 2021
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:
Similar Reads
C# | Copy the entire LinkedList<T> to Array
LinkedList<T>.CopyTo(T[], Int32) method is used to copy the entire LinkedList<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : It is the one-dimensional Array that is the
2 min read
C# | Copying BitArray elements to an Array
The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent
3 min read
C# | Copying the elements of ArrayList to a new array
ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra
2 min read
C# | How to copy the entire ArrayList to a one-dimensional Array
ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList
3 min read
C# | How to insert an element in an Array?
An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C#. Let's say we have an array and we want to insert an element at a specific position in this array. Here's how to do it. First get the element to be inserte
2 min read
Copying the Queue elements to 1-D Array in C#
Queue<T>.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, whe
4 min read
C# | Copying the HybridDictionary entries to an Array Instance
HybridDictionary.CopyTo(Array, Int32) method is used to copy the HybridDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry obje
3 min read
C# | Copying the Hashtable elements to an Array Instance
Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry
3 min read
C# | Copy StringCollection at the specified index of array
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.CopyTo(String[], Int32) method is used to copy the entire StringCollection values
3 min read
C# | Copy the Stack to an Array
Stack<T>.CopyTo(T[], Int32) Method is used to copy the Stack<T> to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stack<T>is the number of elements the Stack<T> can hold. As elements are added to a Stack<T> , the capacit
2 min read