In this guide, we will explore the Buffer.BlockCopy method in C# along with its syntax, arguments, and demonstrations.
What is the Buffer.BlockCopy method?
The Buffer.BlockCopy function in C# offers a robust and efficient approach for performing memory-related tasks at a low level within arrays. This function belongs to the System namespace. It is extensively applied in software requiring direct handling of memory, like graphic programming, network protocols, and processing binary data.
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.
The srcOffsetValue represents the byte offset to the source, starting from zero.
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's consider a scenario to demonstrate the application of the Buffer.BlockCopy method in the C# programming language.
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's consider another instance to demonstrate how the Buffer.BlockCopy method is employed 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