Array Combine Example
This page was last reviewed on Oct 25, 2023.
Dot Net Perls
Combine arrays. In C# two same-typed arrays can be merged. This task is often needed—for further processing in a program, we must combine arrays.
This allows us to use both parts of the data in a single collection. Several approaches are possible. They all have benefits and negatives.
AddRange. Here we use AddRange to combine arrays. This code is unlikely to fail due to off-by-one programming errors—it is reliable and clear.
Part 1 We have 2 integer arrays. The first array has 5 elements, and the second has just 3.
Part 2 We create a new List, and call AddRange twice on it with the 2 arrays as the arguments.
List AddRange
Part 3 We convert the List back into an array. This step can be skipped if we do not need an array.
Part 4 We loop over the resulting integer array by using a foreach-loop and calling Console.WriteLine.
using System; using System.Collections.Generic; // Part 1: declare 2 integer arrays. int[] array1 = { 1, 2, 3, 4, 5 }; int[] array2 = { 6, 7, 8 }; // Part 2: create new List of integers and call AddRange twice. var list = new List<int>(); list.AddRange(array1); list.AddRange(array2); // Part 3: call ToArray to convert List to array. int[] array3 = list.ToArray(); // Part 4: loop through array elements of combined array and print them. foreach (int element in array3) { Console.WriteLine(element); }
1 2 3 4 5 6 7 8
Array.Copy. 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.
Note This pattern of code is the same as that for the Buffer.BlockCopy method shown next: it just uses element counts, not byte counts.
using System; 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); }
4 4 4 5 5
BlockCopy. Next we use Buffer.BlockCopy to merge two int arrays. This method acts upon bytes, not elements (which are four bytes here).
Important We must multiply the element counts by the sizeof(int) to get correct units.
Here We merge two 3-element int arrays. We BlockCopy the first into the "final" array, and then BlockCopy the second.
Tip This version would be the fastest one according to my previous benchmarks of BlockCopy.
using System; // ... Two input arrays. int[] array = { 1, 2, 3 }; int[] array2 = { 4, 5, 6 }; // ... Destination array. int[] final = new int[array.Length + array2.Length]; // ... Copy first array. Buffer.BlockCopy(array, 0, final, 0, array.Length * sizeof(int)); // ... Copy second. // Note the starting offset. Buffer.BlockCopy(array2, 0, final, array.Length * sizeof(int), array2.Length * sizeof(int)); // ... Display. foreach (int value in final) { Console.WriteLine(value); }
1 2 3 4 5 6
AddRange, internals. Here we discuss the internal implementation of the AddRange() method. The AddRange method internally calls InsertRange.
And It calls the fast Array.Copy method to do a bitwise copy. If you call Array.Copy manually, you could improve performance.
Summary. With AddRange on the List we can combine arrays. This method will also work with more than 2 arrays. The arrays must all have the same type of elements.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 25, 2023 (edit).
© 2007-2024 Sam Allen.