The source code to demonstrate the BlockCopy() method in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//Program to demonstrate the BlockCopy() method
//of the array in C#.
using System;
class Demo
{
static void Main()
{
int[] source = {1,2,3,4,5};
int[] destination = new int[5];
int totalLengthInBytes = source.Length * sizeof(int);
Buffer.BlockCopy(source, 0, destination, 0, totalLengthInBytes);
foreach (int items in destination)
{
Console.Write(items+ " ");
}
Console.WriteLine();
}
}
Output:
1 2 3 4 5
Press any key to continue . . .
Explanation:
In the above program, we created two integer arrays source and destination.
int totalLengthInBytes = source.Length * sizeof(int);
In the above code, we find the total number of bytes. Because Length property returns the number of elements in an array and sizeof(int) return the total bytes occupied by an integer and then we multiplied both values and get total length of an array in bytes.
Program:
The source code to demonstrate the BlockCopy() method in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created two integer arrays source and destination.
In the above code, we find the total number of bytes. Because Length property returns the number of elements in an array and sizeof(int) return the total bytes occupied by an integer and then we multiplied both values and get total length of an array in bytes.