In this article, we will discuss the Buffer.BlockCopy method in C# with its syntax, parameters, and examples.
What is the Buffer.BlockCopy method?
The Buffer.BlockCopy method in C# provides a strong and fast technique to conduct low-level, memory-oriented operations on arrays. This method is part of the System.namespace . It is widely used in applications that need direct memory manipulation, such as graphics programming, network protocols, or working with data in binary form.
Syntax:
It has the following syntax:
public static void BlockCopy (Array srcs, int srcOffsetValue, Array dst, int dstOffset, int c);
Parameters:
srcs: It is the source buffer.
srcOffsetValue: It is the zero-based byte offset to src .
DST: It is the endpoint of the buffer.
dstOffset: The zero-based bytes offset to dst .
c: The total number of bytes to be copied.
Exception:
- ArgumentNullException occurs if either src or dst is null.
- ArgumentException occurs if src or dst are not basic arrays, if src contains fewer bytes than srcOffset plus count, and if the amount of bytes in dst is fewer than dstOffset+count.
- If srcOffset, dstOffset, or count is less than zero, an ArgumentOutOfRangeException is thrown.
Example 1:
Let us take an example to illustrate the use of Buffer.BlockCopy method in C#.
Filename: BufferMethod.cs
// Program to show buffering.Method:
//BlockCopy(Array, Int32, Int32)
using System;
class BufferMethod{
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] srcs = {15, 16, 17, 18 };
long[] dests = {17, 18, 14, 20 };
Console.WriteLine("Initial Array values:");
Console.WriteLine();
// Display hexadecimal values
Console.WriteLine("Array elements in the hexadecimal form:");
displayhexvalue(srcs, "srcs");
displayhexvalue(dests, "dests");
// display Individual byte
Console.WriteLine("Individual bytes:");
displaybytes(srcs, "srcs");
displaybytes(dests, "dests");
// copying the specified number of bytes
// using Buffer.BlockCopy() method
Buffer.BlockCopy(srcs, 4, dests, 7, 6);
Console.WriteLine();
Console.WriteLine("Array after operation:");
Console.WriteLine();
// display hexadecimal values
Console.WriteLine("Array element in hexadecimal form:");
displayhexvalue(srcs, "srcs");
displayhexvalue(dests, "dests");
// display hexadecimal value
Console.WriteLine("Individual bytes:");
displaybytes(srcs, "srcs");
displaybytes(dests, "dests");
}
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 values 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 a 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 elements in the hexadecimal form:
srcs:000000000000000F 0000000000000010 0000000000000011 0000000000000012
dests: 0000000000000011 0000000000000012 000000000000000E 0000000000000014
Individual bytes:
srcs:0F 00 00 00 00 00 00 00 10 00 00 00 0000 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dests: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 0E 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
Array after operation:
Array element in hexadecimal form:
srcs: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dests: 0000000000000011 0000000010000000 000000000000000E 0000000000000014
Individual bytes:
srcs: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 1100 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dests: 11 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 0E 00 00 00 00 00 00 00 14 00 00 00 0000 00 00
Example 2:
Let us take another example to illustrate the use of Buffer.BlockCopy method in C#.
FileName: Example2.cs
//Buffer is demonstrated with a C# application.Method: BlockCopy(Array, Int32, // Int32, Int32)
using System;
class BufferMe{
// Main Method
public static void Main()
{
try {
// Declaring src and dest array
long[] src = {15, 16, 17, 40};
long[] dest = {17, 78, 19, 40};
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,26);
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 values 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);
// reverse of array
// 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 0000000000000028 dest: 0000000000000011 000000000000004E 0000000000000013 0000000000000028
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 0011 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00
dest: 11 0000 00 00 00 00 00 4E 00 00 00 00 00 00 00 13 00 00 00 00 0000 00 28 00 00 00 00 00 00 00
src and dst are null:
Exception Thrown: System.ArgumentNullException