How to flatten two 2D arrays into a single 1D array in C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[,] a = {
            { 1, 2 },
            { 3, 4 }
        };

        int[,] b = {
            { 5, 6 },
            { 7, 8 }
        };

        var merged = a.Cast<int>()
                      .Concat(b.Cast<int>())
                      .ToArray();

        Console.WriteLine(string.Join(", ", merged));
    }
}




/*
run:

1, 2, 3, 4, 5, 6, 7, 8

*/

 



answered Jan 25 by avibootz
...