Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 9
    Completely agree. There's too much room for error with Buffer.BlockCopy. Keep it simple, and don't try to squeeze any juice out of your program until you know where the juice is (profiling). Commented Sep 7, 2009 at 19:36
  • 6
    What if you're dealing with a byte[]? Are there any other gotchas with BlockCopy? Commented Sep 9, 2009 at 9:52
  • 4
    @thecoop: if you're dealing with a byte[] then it's probably fine to use BlockCopy, unless the definition of "byte" is later changed to something other than a byte, which would probably have a pretty negative effect on other parts of your code anyway. :) The only other potential gotcha is that BlockCopy just does straight bytes, so it doesn't take endianness into account, but this would only come into play on a non-Windows machine, and only if you'd screwed up the code in the first place. Also, there might be some weird difference if you're using mono. Commented Sep 9, 2009 at 13:46
  • 6
    In my own testing, Array.Copy() is very similar in performance to Buffer.BlockCopy(). Buffer.BlockCopy is consistently < 10% faster for me when dealing with 640 element byte arrays (which is the sort I'm most interested in). But you should do your own testing with your own data, because it'll presumably vary depending on the data, data types, array sizes, and so forth. I should note that both methods are roughly 3x faster than using Array.Clone(), and maybe 20x faster than copying it in a for loop. Commented Sep 20, 2011 at 22:12
  • 5
    @KevinMiller: uh, UInt16 is two bytes per element. If you pass this array to BlockCopy along with the number of elements in the array, of course only half the array will be copied. For this to work properly, you would need to pass the number of elements times the size of each element (2) as the length parameter. msdn.microsoft.com/en-us/library/… and search for INT_SIZE in the examples. Commented Aug 7, 2016 at 18:45