Concatenate two arrays in C#
This post will discuss how to concatenate two arrays in C#. The solution should contain all the elements of the first array, followed by all the second array elements.
1. Using Enumerable.Concat() method
The Enumerable.Concat() method provides a simple way to concatenate multiple arrays in C#. The following example demonstrates the usage of the Concat() method by concatenating two arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; public static class Extension { public static T[] Concatenate<T>(this T[] first, T[] second) { if (first == null) { return second; } if (second == null) { return first; } return first.Concat(second).ToArray(); } } public class Example { public static void Main() { int[] first = { 1, 2, 3 }; int[] second = { 4, 5 }; int[] result = first.Concatenate(second); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
2. Using Array.CopyTo() method
The Array.CopyTo() method provides a convenient and efficient way to copy an array in C#. We can use this method for copying all elements of the given arrays into an empty array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; public static class Extension { public static T[] Concat<T>(this T[] first, params T[] second) { if (first == null) { return second; } if (second == null) { return first; } T[] result = new T[first.Length + second.Length]; first.CopyTo(result, 0); second.CopyTo(result, first.Length); return result; } } public class Example { public static void Main() { int[] first = { 1, 2, 3 }; int[] second = { 4, 5 }; int[] result = first.Concat(second); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
3. Using AddRange() method
We can use the AddRange() method to add the specified array elements at the end of the List<T>. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; public static class Extension { public static T[] Concat<T>(this T[] first, T[] second) { if (first == null) { return second; } if (second == null) { return first; } List<T> list = new List<T>(first.Length + second.Length); list.AddRange(first); list.AddRange(second); return list.ToArray(); } } public class Example { public static void Main() { int[] first = { 1, 2, 3 }; int[] second = { 4, 5 }; int[] result = first.Concat(second); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
The above code can be shortened to a single AddRange() call as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Collections.Generic; public static class Extension { public static T[] Concat<T>(this T[] first, T[] second) { if (first == null) { return second; } if (second == null) { return first; } List<T> list = new List<T>(first); list.AddRange(second); return list.ToArray(); } } public class Example { public static void Main() { int[] first = { 1, 2, 3 }; int[] second = { 4, 5 }; int[] result = first.Concat(second); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
4. Using Array.Resize with Array.Copy
Finally, if you need to merge the second array into the first array, we can resize the first array using the Array.Resize() method and then use Array.Copy to copy elements from the second array to the first array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { public static void Main() { int[] first = { 1, 2, 3 }; int[] second = { 4, 5 }; Array.Resize(ref first, first.Length + second.Length); Array.Copy(second, 0, first, first.Length - second.Length, second.Length); Console.WriteLine(String.Join(",", first)); } } /* Output: 1,2,3,4,5 */ |
That’s all about concatenating two arrays in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)