C# Combine Arrays Example

Array type

Two arrays can be merged. You have two arrays that contain the same types of elements in your C# program. For further processing in your program, you need these two arrays to be combined into a single array reference. This allows you to use both parts of the data in a single collection.

These C# programs show how to combine two 1D arrays into a single array.

Example

Programming tip

First, the solution here concentrates on providing code that is unlikely to fail due to off-by-one programming errors. When you have two arrays, you can use several ways to combine them, including allocating a new array and looping through the two arrays to copy, or using Array.Copy invocations or even Buffer.BlockCopy.

Array.Copy Method Usage Buffer.BlockCopy Method

However, these approaches use loop indexes and can cause subtle bugs. Here we use the List type and its AddRange method and finally invoke ToArray to get the combined array.

List AddRange Use ToArray Extension Method
Program that combines two arrays [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	// ::: Declare two integer arrays with five elements each
	int[] array1 = { 1, 2, 3, 4, 5 };
	int[] array2 = { 6, 7, 8, 9, 10 };

	// ::: Loop through the two arrays and print them
	foreach (int element in array1)
	{
	    Console.WriteLine(element);
	}
	foreach (int element in array2)
	{
	    Console.WriteLine(element);
	}

	// ::: Create new List of integers and call AddRange twice
	var list = new List<int>();
	list.AddRange(array1);
	list.AddRange(array2);

	// ::: Call ToArray to convert List to array
	int[] array3 = list.ToArray();

	// ::: Loop through array elements of combined array and print them
	foreach (int element in array3)
	{
	    Console.WriteLine(element);
	}
    }
}

Output

1       (array1 start) (array3 start)
2
3
4
5
6       (array2 start)
7
8
9
10
Main method

Overview. The program file defines the Main entry point, which contains two array declarations for array1 and array2 at the start of its method body. The two arrays both contain five integers. The code here could be changed to use string[] arrays or any other type.

Using List type. Next the program uses the List constructed type to provide a level of abstraction over the actual copying to the new array. The AddRange method invocations will actually loop through the array1 and array2 elements and copy them into new memory.

List Examples

Implementation

.NET Framework information

Here we discuss the internal implementation of the AddRange method and how it actually combines the arrays in the program text shown. The AddRange method internally calls InsertRange, which validates the arguments before proceeding. Finally, it uses the ICollection generic interface to access the elements and calls the fast Array.Copy method to do a bitwise copy of the values or reference values.

Performance

Performance optimization

The List technique above has some overhead related to casting and parameter validation. You can directly call Array.Copy for good performance, and sometimes even call Buffer.BlockCopy to perform a lightning-fast bitwise copy of the element values (not the reference values).

Array.Resize Changes Array Lengths

Array.Copy example

This example program shows how to use Array.Copy to combine arrays. This is more efficient than the List approach. It only requires a new array. The code becomes more complex with multiple arrays.

Program that uses Array.Copy [C#]

using System;

class Program
{
    static void Main()
    {
	int[] values1 = { 4, 4, 4 };
	int[] values2 = { 5, 5 };

	int[] all = new int[values1.Length + values2.Length];
	Array.Copy(values1, all, values1.Length);
	Array.Copy(values2, 0, all, values1.Length, values2.Length);

	foreach (int value in all)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

4
4
4
5
5

Summary

The C# programming language

We looked at how you can combine two arrays using a useful method from the List constructed type. This method will also work with more than two arrays, but the arrays must all have the same type of elements. We also checked out the Array.Copy method.

Array Types
.NET