0
0
CsharpProgramBeginner · 2 min read

C# Program to Merge Two Arrays Easily

You can merge two arrays in C# by using Concat method from System.Linq namespace like this: var merged = array1.Concat(array2).ToArray(); which combines both arrays into one.
📋

Examples

Inputarray1 = {1, 2}, array2 = {3, 4}
Output{1, 2, 3, 4}
Inputarray1 = {10, 20, 30}, array2 = {40, 50}
Output{10, 20, 30, 40, 50}
Inputarray1 = {}, array2 = {5, 6}
Output{5, 6}
🧠

How to Think About It

To merge two arrays, think of putting all elements of the first array and then all elements of the second array together in one new array. We do not change the original arrays but create a new combined array that holds all values in order.
📐

Algorithm

1
Take the first array as input.
2
Take the second array as input.
3
Create a new array that can hold all elements from both arrays.
4
Copy all elements from the first array into the new array.
5
Copy all elements from the second array into the new array after the first array's elements.
6
Return or print the new merged array.
💻

Code

csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        int[] merged = array1.Concat(array2).ToArray();
        Console.WriteLine("{" + string.Join(", ", merged) + "}");
    }
}
Output
{1, 2, 3, 4, 5, 6}
🔍

Dry Run

Let's trace merging array1 = {1, 2, 3} and array2 = {4, 5, 6} through the code

1

Start with two arrays

array1 = {1, 2, 3}, array2 = {4, 5, 6}

2

Use Concat to combine

array1.Concat(array2) creates a sequence: 1, 2, 3, 4, 5, 6

3

Convert to array

Call ToArray() to make a new array: {1, 2, 3, 4, 5, 6}

4

Print merged array

Output: {1, 2, 3, 4, 5, 6}

StepMerged Array Contents
After Concat1, 2, 3, 4, 5, 6
After ToArray{1, 2, 3, 4, 5, 6}
💡

Why This Works

Step 1: Using Concat

The Concat method joins two sequences one after another without changing the originals.

Step 2: Creating a new array

Calling ToArray() converts the combined sequence into a new array holding all elements.

Step 3: Printing the result

We use string.Join to display the array elements nicely inside curly braces.

🔄

Alternative Approaches

Using Array.Copy
csharp
using System;

class Program {
    static void Main() {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        int[] merged = new int[array1.Length + array2.Length];
        Array.Copy(array1, 0, merged, 0, array1.Length);
        Array.Copy(array2, 0, merged, array1.Length, array2.Length);
        Console.WriteLine("{" + string.Join(", ", merged) + "}");
    }
}
This method manually copies elements and is useful if you want to avoid LINQ or control copying.
Using List and AddRange
csharp
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        List<int> list = new List<int>(array1);
        list.AddRange(array2);
        int[] merged = list.ToArray();
        Console.WriteLine("{" + string.Join(", ", merged) + "}");
    }
}
Using a List allows flexible merging and easy adding of arrays but uses extra memory for the list.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

Merging requires visiting each element of both arrays once, so time grows linearly with total elements.

Space Complexity

A new array is created to hold all elements, so space grows linearly with combined size.

Which Approach is Fastest?

Using Array.Copy is slightly faster than LINQ Concat but less readable; Concat is best for clarity.

ApproachTimeSpaceBest For
LINQ Concat + ToArrayO(n + m)O(n + m)Simple and readable merging
Array.CopyO(n + m)O(n + m)Performance-critical manual copying
List AddRangeO(n + m)O(n + m)Flexible merging with dynamic collections
💡
Use Concat with ToArray() for a quick and readable way to merge arrays.
⚠️
Forgetting to call ToArray() after Concat results in a sequence, not an array.