How to define a rectangular array in C#?

CsharpProgrammingServer Side Programming

Multi-dimensional arrays are also called rectangular array. You can define a 3-dimensional array of integer as −

int [ , , ] a;

Let us see how to define a two-dimensional array −

Int[,] a = new[3,3]

The following is an example showing how to work with a multi-dimensional i.e. rectangular array in C# −

Example

 Live Demo

using System;

namespace Demo {

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

         int[,] a = new int[3, 3];
         a[0,1]= 1;
         a[0,2]= 2;
         a[1,0]= 3;
         a[1,1]= 4;
         a[1,2]= 5;
         a[2,0]= 6;
         a[2,1]= 7;
         a[2,2]= 8;

         int i, j;

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

Output

a[0,0] = 0
a[0,1] = 1
a[0,2] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
a[2,0] = 6
a[2,1] = 7
a[2,2] = 8

raja
Published on 06-Aug-2018 13:02:50
Advertisements