How to merge two arrays in C#

3 Answers

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 6, 7, 8, 9 };

            int[] merge = arr1.Concat(arr2).ToArray();

            Console.WriteLine(string.Join(" ", merge.Cast<int>()));
        }
    }
}


/*
run:
 
1 2 3 4 5 6 7 8 9
 
*/

 



answered Apr 30, 2017 by avibootz
0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 6, 7, 8, 9, 0 };

            int[] merge = arr1.Union(arr2).ToArray();

            Console.WriteLine(string.Join(" ", merge.Cast<int>()));
        }
    }
}


/*
run:
 
1 2 3 4 5 6 7 8 9 0
 
*/

 



answered Apr 30, 2017 by avibootz
0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 6, 7, 8, 9, 0, 10, 11 };

            int[] merge = new int[arr1.Length + arr2.Length];

            Array.Copy(arr1, merge, arr1.Length);
            Array.Copy(arr2, 0, merge, arr1.Length, arr2.Length);

            Console.WriteLine(string.Join(" ", merge.Cast<int>()));
        }
    }
}


/*
run:
 
1 2 3 4 5 6 7 8 9 0 10 11
 
*/

 



answered Apr 30, 2017 by avibootz
...