Home »
.Net »
C# Programs
C# - Array.BlockCopy() Method
Here, we are going to learn about the BlockCopy method and its C# implementation.
Submitted by Nidhi, on August 22, 2020 [Last updated : March 19, 2023]
Array.BlockCopy() Method: Description and Usage
Here we will demonstrate the BlockCopy() method of the array. The BlockCopy() method is used to copy one array to another array.
Syntax
void BlockCopy(
source,
offset_source,
destination,
offse_destination,
totalLengthInBytes
);
Parameter(s):
- source - Source array to be copied.
- offset_source - It specifies the offset, from where data to be copied.
- destination - Destination array.
- offset_destination - It specifies the offset, from where data will be copied.
- totalLengthInBytes - It specifies the total bytes to be copied.
C# program to demonstrate the example of Array.BlockCopy() Method
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.
C# Basic Programs »