How To Join Two Arrays In C#

Introduction

This article will teach different methods to join two arrays in C#. To learn everything about arrays in C#, check out Become a Master of Working With C# Arrays - A Comprehensive Guide to Arrays (c-sharpcorner.com)

What are Arrays in C#

An array in C# is a list of items of the same type stored together in memory. These elements can be accessed using an index, which starts from zero. Arrays are used to store multiple values under a single variable name. In C#, arrays can be either single-dimensional or multi-dimensional.

How to join two arrays in C#

We have three methods to join two arrays in C#, which are given below:

  1. Using Concat method
  2. Using a Loop
  3. Using CopyTo Method

Method 1. Using Concat Method

You can join two arrays in C# using the Concat method of the array. The Concat method joins two arrays into a single array. The method returns an IEnumerable that contains the elements of both arrays.

For example, we have two arrays (array1 and array2) with some elements, as given in the below code snippet.

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 4, 5, 6 };

int[] result = array1.Concat(array2).ToArray();

Console.WriteLine("Result Array: [{0}]", string.Join(", ", result));

The output of the above code snippet is shown in Figure 1.

Method 2. Using a Loop

Join two arrays in C# by using a loop. You can create a new array with a size equal to the sum of the lengths of both arrays, and then loop through both arrays and copy the elements from each array into the new array. 

For example, we have two arrays (array1 and array2) with some elements, as given in the below code snippet.

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 4, 5, 6 };

int[] result = new int[array1.Length + array2.Length];
for (int i = 0; i < array1.Length; i++) {
    result[i] = array1[i];
}
for (int i = 0; i < array2.Length; i++) {
    result[array1.Length + i] = array2[i];
}
Console.WriteLine("Result Array: [{0}]", string.Join(", ", result));

The output of the above code snippet is shown in Figure 2.

Method 3. Using CopyTo Method

The CopyTo method is used to copy the elements of an array to a specified array.

For example, we have two arrays (array1 and array2) with some elements, as given in the below code snippet.

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 4, 5, 6 };

int[] result = new int[array1.Length + array2.Length];

array1.CopyTo(result, 0);
array2.CopyTo(result, array1.Length);

Console.WriteLine("Result Array: [{0}]", string.Join(", ", result));

The output of the above code snippet is shown in Figure 3.

Why We Join Two Arrays in C#

Joining two arrays in C# is a useful operation that can simplify the process of working with multiple arrays. It is useful in below conditions:

  • When you have two arrays and want to merge them into a single array.
  • When you need to concatenate the elements of two arrays. 
  • Improving performance: Joining two arrays can sometimes be more efficient than processing multiple arrays separately, especially if the arrays are large.

Conclusion

This article has taught us various ways to join arrays in C#. Understanding these methods will help you to write efficient and error-free code. 

To learn about How To Compare Two Arrays In C# and How To Check If An Array Is Empty In C#, check out below articles

If you require any clarification/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about C# Programming or to explore more technologies.

Thanks for reading, and I hope you like it.


Recommended Free Ebook
Similar Articles