I am creating several byte arrays that need to be joined together to create one large byte array – i’d prefer not to use byte[]’s at all but have no choice here…
I am adding each one to a List as I create them, so I only have to do the concatenation once I have all the byte[]’s, but my question is, what is the best way of actually doing this?
When I have a list with an unknown number of byte[]’s and I want to concat them all together.
Thanks.
The above code will concatenate a sequence of sequences of bytes into one sequence – and store the result in an array.
Though readable, this is not maximally efficient – it’s not making use of the fact that you already know the length of the resultant byte array and thus can avoid the dynamically extended
.ToArray()implementation that necessarily involves multiple allocations and array-copies. Furthermore,SelectManyis implemented in terms of iterators; this means lots+lots of interface calls which is quite slow. However, for small-ish data-set sizes this is unlikely to matter.If you need a faster implementation you can do the following:
or as Martinho suggests:
Some timings:
Using the short method to concatenate these 500500 bytes takes 15ms, using the fast method takes 0.5ms on my machine – YMMV, and note that for many applications both are more than fast enough ;-).
Finally, you could replace
Array.CopyTowith thestaticArray.Copy, the low-levelBuffer.BlockCopy, or aMemoryStreamwith a preallocated back buffer – these all perform pretty much identically on my tests (x64 .NET 4.0).