Concatenate two sequences in C#
This post will discuss how to concatenate two sequences in C#.
1. Using Enumerable.Concat
Method
The recommended solution to concatenate two sequences in C# is using LINQ’s Enumerable.Concat method. The following code example demonstrates its usage to concatenate two arrays. It returns all elements in the input sequences, in the same order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int[] result = first.Concat(second).ToArray(); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7 } } |
2. Using Array.CopyTo
Method
Another option is to allocate memory for the new array to accommodate all the elements of both arrays. Then use the Array.CopyTo() method to concatenate both arrays. The following code example demonstrates this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int[] result = new int[first.Length + second.Length]; first.CopyTo(result, 0); second.CopyTo(result, first.Length); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7 } } |
The solution can be easily extended to concatenate an arbitrary number of arrays of the same type, 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 |
using System; using System.Linq; public class Example { public static T[] Concat<T>(params T[][] arrays) { var result = new T[arrays.Sum(a => a.Length)]; int offset = 0; foreach (T[] array in arrays) { array.CopyTo(result, offset); offset += array.Length; } return result; } public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int[] third = { 8, 9, 10 }; int[] result = Concat(first, second, third); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7, 8, 9, 10 } } |
3. Using Array.Copy
Method
The Array.Copy()
method can also work here. The following code example shows how it can be used to copy elements from each of the arrays at the end of the destination array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int[] result = new int[first.Length + second.Length]; Array.Copy(first, result, first.Length); Array.Copy(second, 0, result, first.Length, second.Length); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7 } } |
If you need to add contents of an array to the end of another array, resize the array before performing the copy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int firstLen = first.Length; Array.Resize(ref first, firstLen + second.Length); Array.Copy(second, 0, first, firstLen, second.Length); Console.WriteLine(String.Join(", ", first)); // 1, 4, 5, 6, 7 } } |
4. Using Buffer.BlockCopy
Method
For faster and efficient copying, consider using the Buffer.BlockCopy method over the CopyTo()
or the Copy()
method. It copies the specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; int[] result = new int[first.Length + second.Length]; var byteIndex = first.Length * sizeof(int); Buffer.BlockCopy(first, 0, result, 0, byteIndex); Buffer.BlockCopy(second, 0, result, byteIndex, second.Length * sizeof(int)); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7 } } |
5. Using List<T>.AddRange
Method
Finally, we can add elements of all the given arrays to the end of a List using the List<T>.AddRange
method. Then, convert the list back to an array, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { int[] first = { 1, 4, 5 }; int[] second = { 6, 7 }; List<int> list = new List<int>(); list.AddRange(first); list.AddRange(second); int[] result = list.ToArray(); Console.WriteLine(String.Join(", ", result)); // 1, 4, 5, 6, 7 } } |
That’s all about concatenating two sequences 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 :)