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.

Download  Run Code

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:

Download  Run Code

 
The solution can be easily extended to concatenate an arbitrary number of arrays of the same type, as shown below:

Download  Run Code

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.

Download  Run Code

 
If you need to add contents of an array to the end of another array, resize the array before performing the copy:

Download  Run Code

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.

Download  Run Code

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.

Download  Run Code

That’s all about concatenating two sequences in C#.