C#
arrays
concatenate
programming
duplicate

How do I concatenate two arrays in C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Concatenating two arrays in C# means building a new sequence that contains the elements of the first array followed by the elements of the second. Because arrays have fixed size, concatenation always creates a new array or uses another collection type as an intermediate step.

Use LINQ for the Clearest Code

The most concise approach is Concat from LINQ followed by ToArray.

csharp
1using System;
2using System.Linq;
3
4class Program
5{
6    static void Main()
7    {
8        int[] first = { 1, 2, 3 };
9        int[] second = { 4, 5, 6 };
10
11        int[] combined = first.Concat(second).ToArray();
12        Console.WriteLine(string.Join(", ", combined));
13    }
14}

This reads well and works for any element type. It is often the best answer for everyday application code where clarity matters more than micro-optimization.

Use Array.Copy for Explicit Control

If you want to avoid LINQ and copy the data directly, create a destination array and copy each source into it.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] first = { 1, 2, 3 };
8        int[] second = { 4, 5, 6 };
9        int[] combined = new int[first.Length + second.Length];
10
11        Array.Copy(first, 0, combined, 0, first.Length);
12        Array.Copy(second, 0, combined, first.Length, second.Length);
13
14        Console.WriteLine(string.Join(", ", combined));
15    }
16}

This is a good fit when you want a straightforward fixed-size array result without involving LINQ.

List<T> Is Useful When You Are Building Incrementally

If you are already working with dynamic collections, a list can make the operation more flexible.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6    static void Main()
7    {
8        int[] first = { 1, 2, 3 };
9        int[] second = { 4, 5, 6 };
10
11        List<int> list = new List<int>(first.Length + second.Length);
12        list.AddRange(first);
13        list.AddRange(second);
14
15        int[] combined = list.ToArray();
16        Console.WriteLine(string.Join(", ", combined));
17    }
18}

This is especially convenient if concatenation is only one step in a longer process of adding, filtering, or transforming elements.

Arrays Are Fixed-Size, So Mutation Has Limits

A common source of confusion is expecting one array to grow when the second is appended. That is not how arrays work in C#. An array cannot resize itself.

That means these are your real choices:

  • create a new array containing both inputs
  • use a list or another resizable collection
  • stay lazy as IEnumerable<T> if you do not actually need an array yet

Understanding that constraint helps you choose the right abstraction earlier.

Lazy Concatenation With IEnumerable<T>

If you only need to iterate later and do not need a concrete array immediately, you can keep the result lazy.

csharp
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5class Program
6{
7    static void Main()
8    {
9        int[] first = { 1, 2, 3 };
10        int[] second = { 4, 5, 6 };
11
12        IEnumerable<int> combined = first.Concat(second);
13        foreach (int value in combined)
14        {
15            Console.WriteLine(value);
16        }
17    }
18}

This can be useful when concatenation is part of a larger LINQ pipeline and materializing an array immediately would be unnecessary.

Performance Considerations

For most code, both LINQ and Array.Copy are perfectly fine. If you are doing this in a hot path with large arrays, Array.Copy gives you more explicit control and avoids the extra abstraction of a LINQ pipeline.

But performance decisions should be measured, not assumed. In ordinary business code, the readability of Concat(...).ToArray() is often worth more than shaving off a tiny amount of overhead.

Generic Types Work the Same Way

All of the same approaches work for arrays of strings, custom objects, or any other type.

csharp
string[] first = { "a", "b" };
string[] second = { "c", "d" };
string[] combined = first.Concat(second).ToArray();

Concatenation is not special to numeric arrays.

Common Pitfalls

A common mistake is expecting the first array to expand in place. Arrays in C# have fixed length.

Another mistake is using LINQ and forgetting ToArray, then wondering why the result is still only an IEnumerable<T>.

Developers also sometimes choose a complex manual loop when Concat or Array.Copy would be clearer and less error-prone.

Finally, if the code performs repeated concatenation in a loop, an array may be the wrong data structure. A List<T> is often the better intermediate container.

Summary

  • Arrays in C# are fixed-size, so concatenation creates a new result.
  • 'first.Concat(second).ToArray() is the cleanest everyday solution.'
  • 'Array.Copy is a clear manual alternative when you want explicit control.'
  • 'List<T> is helpful when you are building a larger collection incrementally.'
  • Keep the result lazy as IEnumerable<T> if you do not need an actual array yet.

Course illustration
Course illustration

All Rights Reserved.