How to convert a jagged array to a one-dimensional array in C#

1 Answer

0 votes
using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string[][] jaggedArray =  { 
            new string[] {"a", "b", "c", "d"}, 
            new string[] {"e", "f"}, 
            new string[] {"g", "h", "i"} };

        var result = jaggedArray.SelectMany(s => s).Cast<object>().ToArray();

        Console.Write(string.Join(", ", result));
    }
}



/*
run:

a, b, c, d, e, f, g, h, i

*/

 



answered Mar 13, 2025 by avibootz
...