How to convert matrix to array in C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        int[,] matrix = { {1,   2,   3},
                          {5,   6, 103},
                          {2, 102,   4},
                          {8,   7, 106},
                          {9,  10,  11} };
 
        var array = matrix.Cast<int>().ToArray();

        foreach (int item in array) {
            Console.WriteLine(item);
        }
    }
}




/*
run:

1
2
3
5
6
103
2
102
4
8
7
106
9
10
11

*/

 



answered Sep 7, 2023 by avibootz
edited Sep 7, 2023 by avibootz
...