47

There are two byte arrays which are populated with different values.

byte[] Array1 = new byte[5];
byte[] Array2 = new byte[5];

Then, I need Array1 to get exactly the same values as Array2.

By typing Array1 = Array2 I would just set references, this would not copy the values.

What might be the solution?

EDIT:

All answers are good and all solutions work. The code from the first solution looks visually more descriptive for my particular case.

Array1 = Array2.ToArray();

and

Array1.CopyTo(Array2, 0);

as well as

Buffer.BlockCopy(Array2, 0, Array1, 0, 5);

4

2 Answers 2

60

One solution courtesy of Linq...

Array1 = Array2.ToArray();

EDIT: you do not need to allocate space for Array1 before using this Linq call. The allocation for Array1 is done within ToArray(). More complete example below

byte[] Array2 = new byte[5];
// set values for Array2
byte[] Array1 = Array2.ToArray();
Sign up to request clarification or add additional context in comments.

5 Comments

This is a fairly neat solution, but since the OP has allocated the destination array already and has said, "I just set references, I don't copy the values", then I think it would be prudent to point out in your answer that you are creating a new reference and not copying the values to the pre-allocated array.
Will do, the statement in the OP "By typing Array1 = Array2 I just set references, I don't copy the values" I thought was an attempt to say that the array reference was copied, and that the values themselves were not duplicated. This also seemed to be the case in his later comment "But it should not be linked in any way with it.", which I thought was an attempt to say Array1[0] = (byte) 1 will do nothing to the values in Array2
Yes, I agree. That's why I think your answer is a fairly neat solution. I just thought that the OP might need to understand the mechanics a little more deeply.
As I understood, Array2.ToArray() allocates a new array. What if Array1 = Array2.ToArray(); is executed multiple times, in a for loop for example? Does these allocations remain in memory?
@acoder C# has a garbage collection system, so if you have no more references to allocated memory, then the values will be cleaned up. I wouldn't recommend allocating new memory in a loop just to ignore the values quickly thereafter.
50
Array2.CopyTo(Array1, 0);

Microsoft Docs

3 Comments

Six years later, I feel it's worth pointing out that this should be Array2.CopyTo(Array1, 0). The line as given does the copy in the opposite direction to what was asked.
@Kemp, yep, fixed.
A little more details would've been helpful. The integer parameter is from which element it should copy. Microsoft docs are unreadable as always. I believe Array1 needs to be pre-allocated, I'll find out soon enough :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.