Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
HI, say I have an array
byte[] a = File.ReadAllBytes("a.doc");
byte[] b = File.ReadAllBytes("b.doc"); 
how to combine these 2 array into one array in such a way that both array should be there on new array?
Posted 16-Jan-13 3:42am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use a list and add each array using AddRange then convert back to an array
List<byte> list = new List<byte>();
byte[] c= list.ToArray();
Faisalabadians at 16-Jan-13 9:52am
I am talking about byte[] not byte
CHill60 at 16-Jan-13 10:14am
That was my understanding of your question ...that's why the solution posted produces a byte[]
Marcus Kramer at 16-Jan-13 10:38am
My 5. Absolutely correct.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi Faisalabadians.
about the solution of CHill60 - nice & simple (you got my 5+)
If performance is important and if you can use an IEnumerable<byte>, definitely prefer Linq's Concat<> method.
IEnumerable<byte> Con_A_and_B = a.Concat(b);
Andreas Gieriet at 20-Jan-13 10:39am
My 5! That's also like I would do it (I would even use var, but that is debatable for many here... ;-)).
Edo Tzumer at 21-Jan-13 4:16am
tnx Andreas :)
CHill60 at 21-Jan-13 4:13am
You're getting my +5 for this as well - about time I did more with Linq. Oh the joy of working with legacy versions of tools ;-p
Edo Tzumer at 21-Jan-13 4:16am
Thank you Chill ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Checkout these two links for your answer:
How to combine two byte arrays
Best way to combine two or more byte arrays in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 514
1 Gihan Liyanage 298
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 185
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928

Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100