What is the simplest multi-dimensional array in C#?


The simplest multi-dimensional array in C# is a two-dimensional array. A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.

Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 4 rows and each row has 4 columns.

int [,] a = new int [4,4] {
   {0, 1, 2, 3} , /* initializers for row indexed by 0 */
   {4, 5, 6, 7} , /* initializers for row indexed by 1 */
   {8, 9, 10, 11} /* initializers for row indexed by 2 */
   {12, 13, 14, 15} /* initializers for row indexed by 3 */
};

The following is an example −

Example

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {

         int[,] a = new int[5, 2] {{77,34}, {55,65}, {47,66}, {45,98}, {86,23} };
         int i, j;

         for (i = 0; i < 5; i++) {
   
            for (j = 0; j < 2; j++) {
               Console.WriteLine(a[i,j]);
            }
         }
         Console.ReadKey();
      }
   }
}

Updated on: 21-Jun-2020

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements