Merging two arrays in .NET

In C# 3.0 you can use LINQ’s Concat method to accomplish this easily:

int[] front = { 1, 2, 3, 4 };
int[] back = { 5, 6, 7, 8 };
int[] combined = front.Concat(back).ToArray();

In C# 2.0 you don’t have such a direct way, but Array.Copy is probably the best solution:

int[] front = { 1, 2, 3, 4 };
int[] back = { 5, 6, 7, 8 };

int[] combined = new int[front.Length + back.Length];
Array.Copy(front, combined, front.Length);
Array.Copy(back, 0, combined, front.Length, back.Length);

This could easily be used to implement your own version of Concat.

Leave a Comment