How to initialize three dimensional int array in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void Main() {
        int[,,] array3d = new int[,,] { { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } },
                                        { { 10, 20, 30, 40 }, { 50, 60, 70, 80 } },
                                        { { 9, 10, 11, 12 }, { 13, 14, 15, 16 } } };
 
        for (int i = 0; i < array3d.GetLength(0); i++) {
            for (int j = 0; j < array3d.GetLength(1); j++) {
                for (int k = 0; k < array3d.GetLength(2); k++) {
                    Console.Write("{0} ", array3d[i, j, k]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}
 
 
 
 
/*
run:
 
1 2 3 4 
5 6 7 8 

10 20 30 40 
50 60 70 80 

9 10 11 12 
13 14 15 16 
 
*/

 



answered Nov 25, 2020 by avibootz

Related questions

1 answer 121 views
2 answers 153 views
1 answer 196 views
...