开发者

Most efficient way to merge two byte arrays into one byte array? [duplicate]

This question already has answers here: Best way to combine two or more byte arrays in C# 开发者_Go百科 (13 answers) Closed 9 years ago.

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#

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜