Most efficient way to merge two byte arrays into one byte array? [duplicate]
I have two byte arrays. I want to merge these two byte arrays into one byte array.
Usually, I just create a new byte array with length = byte array #1 + byte array #2. Then copy byte array #1 and #2 to the new byte array.
Is there a more efficient way to merge two byte arrays using VB.NET and .NET 4?
Your existing approach is the most efficient (for what I think is the commonly understood meaning of "efficient") as long as it is implemented properly.
The implementation should look like this:
var merged = new byte[array1.Length + array2.Length];
array1.CopyTo(merged, 0);
array2.CopyTo(merged, array1.Length);
In our Tcpclient we like to use Buffer.BlockCopy instead of array.copy.
See this question for more info: Array.Copy vs Buffer.BlockCopy
And this one for hard numbers: Best way to combine two or more byte arrays in C#
精彩评论