Click here to Skip to main content
Rate this: bad
good
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
good
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>();
list.AddRange(a);
list.AddRange(b);
 
byte[] c= list.ToArray();
  Permalink  
Comments
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
good
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);
 
Cheers,
Edo
  Permalink  
v2
Comments
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... ;-)).
Cheers
Andi
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
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 620
1 Maciej Los 610
2 CPallini 255
3 RyanDev 161
4 Peter Leow 125
0 OriginalGriff 5,784
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,009
3 Maciej Los 2,894
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web03 | 2.8.140721.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