Array concatenation in C#

Betamoo picture Betamoo · May 7, 2010 · Viewed 19.2k times · Source
  1. How do I smartly initialize an Array with two (or more) other arrays in C#?

    double[] d1 = new double[5];
    double[] d2 = new double[3];
    double[] dTotal = new double[8]; // I need this to be {d1 then d2}
    
  2. Another question: How do I concatenate C# arrays efficiently?

Answer

Julien Hoarau picture Julien Hoarau · May 7, 2010

You could use CopyTo:

double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.Length + d2.Length];

d1.CopyTo(dTotal, 0);
d2.CopyTo(dTotal, d1.Length);