
Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hello. How I can copy one byte array to other byte array?
>
[quoted text clipped - 7 lines]
> How to do this? There are should be some function which work like "copy"
> function in C++.
> SushiSean,
>
> You would use the static BlockCopy method on the Buffer class, like so:
>
> Buffer.BlockCopy(array1, 55, 51, array2, 15, 51);
Not to dispute the usefulness of Buffer.BlockCopy(), but it seems to me
that in this case, where one is simply copying from one array to
another, the Array.Copy() method would be preferable (if nothing else,
just from a readability point of view):
Array.Copy(array1, 55, array2, 15, 51);
Also, I think that if one is using Buffer.BlockCopy(), the correct
parameter list is this (basically the same as Array.Copy()):
Buffer.BlockCopy(array1, 55, array2, 15, 51);
Any particular reason for preferring Buffer.BlockCopy()? I prefer
Array.Copy() because it's a method that already exists in the class for
the data type being used (even if it is a static method). Is there
something about BlockCopy() that makes it better?
Pete
Ignacio Machin ( .NET/ C# MVP ) - 09 Oct 2007 19:01 GMT
Hi,
>> SushiSean,
> Any particular reason for preferring Buffer.BlockCopy()? I prefer
> Array.Copy() because it's a method that already exists in the class for
> the data type being used (even if it is a static method). Is there
> something about BlockCopy() that makes it better?
I think I read somewhere (or maybe here in an older post) that BlockCopy
simply use an equivalent of memcpy, on other words it just copy a chunk of
bytes.
Array.Copy takes into account the type of the array (to deal with different
sizes in memory). Basically Array.Copy you go by index, wheater in BlockCopy
is just a byte copying method.
So BlockCopy should be faster.
Disclaimer:
The above is from memory, I haven't check the docs nor the dissassambled
code to check it.