Buffer.BlockCopy does not work on double array?

the following code

double[] src = new[] { 1d, 1d, 2d };
double[] dst = new[] { 0d, 0d, 0d };
Buffer.BlockCopy(src, 0, dst, 0, 3);
for (int i = 0; i < 3; i++)
{
    Console.Write(src[i] + " ");
}
Console.WriteLine();
for (int i = 0; i < 3; i++)
{
    Console.Write(dst[i] + " ");
}
Console.WriteLine();

outputs

1 1 2 
0 0 0

i expected the same value on both lines?

https://dotnetfiddle.net/AH2ebt

Jon Skeet
people
quotationmark

You're telling BlockCopy to copy 3 bytes. You want

Buffer.BlockCopy(src, 0, dst, 0, 24);

or

Buffer.BlockCopy(src, 0, dst, 0, 3 * sizeof(double));

... in order to copy all 24 bytes. The fifth parameter of Buffer.BlockCopy is documented as:

The number of bytes to copy.

Not the number of array elements to copy.

people

See more on this question at Stackoverflow