A simple console application in which you can get how to join 2 arrays in C#. In this demo we have implemented multiple way to merge arrays.
Output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayJoinDemo { class Program { static void Main(string[] args) { int[] array1 = new int[] { 2, 1, 3 }; int[] array2 = new int[] { 5, 4, 6 }; Console.WriteLine("-----Method : 1--------------------"); var join1 = array1.Union(array2).ToArray(); Console.WriteLine(string.Join(",", join1)); Console.WriteLine("-----Method : 2--------------------"); var join2 = array1.Concat(array2).ToArray(); Console.WriteLine(string.Join(",", join2)); Console.WriteLine("-----Method : 3--------------------"); int[] join3 = new int[array1.Length + array2.Length]; Array.Copy(array1, join3, array1.Length); Array.Copy(array2, 0, join3, array1.Length, array2.Length); Console.WriteLine(string.Join(",", join3)); Console.WriteLine("-----Method : 4--------------------"); int[] join4 = new int[array1.Length + array2.Length]; Buffer.BlockCopy(array1, 0, join4, 0, array1.Length * sizeof(int)); Buffer.BlockCopy(array2, 0, join4, array1.Length * sizeof(int), array2.Length * sizeof(int)); Console.WriteLine(string.Join(",", join4)); Console.WriteLine("-----Method : 5--------------------"); int[] join5 = new int[array1.Length + array2.Length]; for (int i = 0; i < array1.Length; i++) { join5[i] = array1[i]; } for (int i = 0; i < array2.Length; i++) { join5[array1.Length + i] = array2[i]; } Console.WriteLine(string.Join(",", join5)); Console.Read(); } } }
Output: