Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI, say I have an array

C#
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

Use a list and add each array using AddRange then convert back to an array
XML
List<byte> list = new List<byte>();
list.AddRange(a);
list.AddRange(b);

byte[] c= list.ToArray();
 
Share this answer
 
Comments
Faisalabadians 16-Jan-13 9:52am    
I am talking about byte[] not byte
CHill60 16-Jan-13 10:14am    
That was my understanding of your question ...that's why the solution posted produces a byte[]
fjdiewornncalwe 16-Jan-13 10:38am    
My 5. Absolutely correct.
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.

C#
IEnumerable<byte> Con_A_and_B = a.Concat(b);


Cheers,
Edo
 
Share this answer
 
v2
Comments
Andreas Gieriet 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
Joezer BH 21-Jan-13 4:16am    
tnx Andreas :)
CHill60 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
Joezer BH 21-Jan-13 4:16am    
Thank you Chill ;)
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900