Merge Two Arrays in C#
-
Merge Two Arrays With the
Array.Copy()
Method in C -
Merge Two Arrays With the
Array.Resize()
Method in C - Merge Two Arrays With the LINQ Method in C
This tutorial will discuss the methods for merging two arrays in C#.
Merge Two Arrays With the Array.Copy()
Method in C
The Array.Copy()
method copies a range of elements from one array to another. We can use the Array.Copy()
method to copy both arrays’ elements to a third merged array. The following code example shows us how to merge two arrays with the Array.Copy()
method in C#.
using System;
namespace merge_arrays
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6 };
int[] arr3 = new int[arr1.Length + arr2.Length];
Array.Copy(arr1, arr3, arr1.Length);
Array.Copy(arr2, 0, arr3, arr1.Length, arr2.Length);
foreach(var e in arr3)
{
Console.WriteLine(e);
}
}
}
}
Output:
1
2
3
4
5
6
In the above code, we initialized 2 arrays of integer values arr1
and arr2
. We declared a third array arr3
to hold the combined elements of arr1
and arr2
. The length of the arr3
array is the sum of lengths of arr1
and arr2
arrays. Then we copied the contents of arr1
and arr2
arrays to the arr3
array with the Array.Copy()
method in C#.
Merge Two Arrays With the Array.Resize()
Method in C
In the previous example, we have to create a separate third array to store both arrays’ merged values. If we want to achieve this goal without creating another array, we have to use the Array.Resize()
method on one of the two arrays. The Array.Resize()
method is used to resize a one-dimensional array in C#. The Array.Resize()
method takes the reference to the array and the desired size as its arguments and resizes the array. The following code example shows us how to merge two arrays with the Array.Resize()
method in C#.
using System;
namespace merge_arrays
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6 };
int array1OriginalLength = arr1.Length;
Array.Resize<int>(ref arr1, array1OriginalLength + arr2.Length);
Array.Copy(arr2, 0, arr1, array1OriginalLength, arr2.Length);
foreach (var e in arr1)
{
Console.WriteLine(e);
}
}
}
}
Output:
1
2
3
4
5
6
We initialized 2 arrays of integer values, arr1
and arr2
. We then resized the arr1
array to hold the values of both arr1
and arr2
with the Array.Resize()
method. We then copied the elements of the arr2
to the arr1
with the Array.Copy()
method in C#.
Merge Two Arrays With the LINQ Method in C
The LINQ or language integrated query integrates the query functionality in data structures in C#. We can use the Concat()
function to merge the elements of two arrays. The Concat(x)
function concatenates the elements of the parameter x
at the end of the calling object in C#. We can then use the ToArray()
function to convert the result to an array. The following code example shows us how we can merge two arrays with the LINQ method in C#.
using System;
using System.Linq;
namespace merge_arrays
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6 };
arr1 = arr1.Concat(arr2).ToArray();
foreach (var e in arr1)
{
Console.WriteLine(e);
}
}
}
}
Output:
1
2
3
4
5
6
We initialized 2 arrays of integer values, arr1
and arr2
. We then concatenated the elements of the arr2
array at the end of the arr1
array with the Array.Concat()
method. We then converted the outcome of this operation to an array with the ToArray()
function in C#.