-1

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?

5
  • 7
    Can you post your code? There are many ways to copy the two arrays, we won't know if you're doing it efficiently until we see you code. Commented Sep 20, 2011 at 8:14
  • Eh, my explanation is clear enough Commented Sep 20, 2011 at 8:15
  • 6
    @NanoWhite, To me it seems you have a poor attitude towards people who are willing to help you and blaming them for your poor attitude. In fact, like the way you are behaving now Id rather not have you in the SO community, we were all willing to help you but all you have is a awful way of treating us. Commented Sep 20, 2011 at 8:58
  • 3
    Obviously, your explanation was not clear enough. Please don't do this again. Locking due to the noise I found in the history. Note Many comments have been removed from this question. Commented Sep 20, 2011 at 10:09
  • stackoverflow.com/questions/415291/… Commented Jul 27, 2017 at 13:07

2 Answers 2

12

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

An answer to the duplicate-related claims System.Buffer.BlockCopy is faster (for byte).
9

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#

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.