On 26 Mar, 11:35, "miher" <feher.zs...@gmail.com> wrote:
> "Jono" <jono.p...@gmail.com> wrote in message
>
> news:de036da2-1fb4-459b-aef7-(E-Mail Removed)...
>
>
>
> > Hi Everyone,
> > I've read the documentation on MSDN regarding Buffer's methods being
> > faster than Array's for primitive types. So I gave it a try, but it
> > doesn't copy all the data I ask it to (it stops after 7 items). I have
> > a C# code sample, where I create an array with 32k items, and set the
> > value of each item to its index in the array... then I use both
> > Buffer.BlockCopy and Array.Copy to copy the first 25 items into a sub
> > array:
>
> > <code>
> > * * * * * * * *int[] source = new int[short.MaxValue];
> > * * * * * * * *for (int index = 0; index < source.Length; index++)
> > * * * * * * * *{
> > * * * * * * * * * *source[index] = index;
> > * * * * * * * *}
> > * * * * * * * *int count = 25;
>
> > * * * * * * * *int[] bufferBlockCopy = new int[count];
> > * * * * * * * *Buffer.BlockCopy(source, 0, bufferBlockCopy, 0,
> > count);
> > * * * * * * * *Print(bufferBlockCopy);
>
> > * * * * * * * *int[] arrayCopy = new int[count];
> > * * * * * * * *Array.Copy(source, 0, arrayCopy, 0, count);
> > * * * * * * * *Print(arrayCopy);
> > </code>
>
> > bufferBlockCopy has zeros after index 7, while arrayCopy has all the
> > correct indexes.
>
> > Have I fundamentally misunderstood the usage pattern of
> > Buffer.BlockCopy or is there some kind of bug in the framework (or my
> > test) code?
>
> > Many thanks,
>
> > Jono
>
> Hi,
>
> BlockCopy expects the number of bytes to copy, not the number of elements..
>
> -Zsolt
Haha! Brilliant. Thank you for letting me know so quickly. Regards,
Jono
|