Easy way to join arrays?

Legend

Newcomer
Joined
Jan 4, 2005
Messages
11
Does the .Net framework have any built-in support for joining arrays?

I couldn't find anything useful, and the things I tried didn't work. Is the only way to do it by adding each individual element?

C#:
byte[] bFirstArray = new byte[6] { 1, 2, 3, 4, 5, 6 };
byte[] bSecondArray = new byte[6] { 7, 8, 9, 10, 11, 12 };
byte[] bThirdArray = null;

//bThirdArray = (byte[])(bFirstArray.ToString() +  bSecondArray.ToString());  //Doesn't work.
//bThirdArray = new byte[] { bFirstArray, bSecondArray }; //Doesn't work.
 
As far as I know, your best bet is to manually combine the arrays...
C#:
  byte[] Array1 = { 1, 2, 3, 4, 5, 6 };
  byte[] Array2 = { 7, 8, 9, 10, 11, 12 };
  byte[] Array3 = new byte[12];
  Array1.CopyTo(Array3, 0);
  Array2.CopyTo(Array3, Array1.Length);

Unless you want to create your own function to do it for you...
C#:
// Haven't tested this function
static System.Array JoinArrays(System.Array Array1, System.Array Array2) {
    // Throw exception if array types don't match
    if(! Array1.GetType().Equals(Array2.GetType())) {
        throw new ArgumentException("Both arrays must be the same type");
    }
    
    // Create resulting array
    System.Array result = System.Array.CreateInstance(
        Array1.GetType(), Array1.Length + Array2.Length);
    
    // Copy both arrays, end to end, in the resulting array
    Array1.CopyTo(result, 0);
    Array2.CopyTo(result, Array1.Length);
    
    // Return the combined arrays
    return result;
}
 
marble_eater said:
As far as I know, your best bet is to manually combine the arrays...
Fantastic - thanks. I haven't tried the second method, but I'll give it a go tonight.

The 'CopyTo' method is the sort of thing I was looking for - I was thinking that I was going to have to enumerate the array elements and copy them individually (or work with some horrendous copymemory hack like I used to do for VBA :()
 
marble_eater said:
C#:
    // Create resulting array
    System.Array result = System.Array.CreateInstance(
        Array1.GetType(), Array1.Length + Array2.Length);
Hmm. For some reason, that's creating an instance of {byte[][]} type array, when passed a {byte[]} array. Not sure why - the debugger is quite clearly showing that Array1 type is {byte[]}.

Maybe I should bo it explicitly and overload the JoinArray function for the different types that I might be using?
 
Yeah, so createinstance will attempt to create an array of byte arrays instead of an array of bytes...
Perhaps, you'll want to do Array1.Item(0).GetType or something similar.
 
Iceplug said:
Yeah, so createinstance will attempt to create an array of byte arrays instead of an array of bytes...
Perhaps, you'll want to do Array1.Item(0).GetType or something similar.
Yeah - finally realised that!

C#:
System.Array result = System.Array.CreateInstance(Array1.GetValue(0).GetType(), Array1.Length + Array2.Length);

(and a check that there is actually and element(0) in the array....
 
Back
Top