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