array-of- arrays double [][] in C#?

CsharpServer Side ProgrammingProgramming

Arrays of arrays in C# are known as Jagged Arrays. To declare jagged arrays, use the double [ ][ ].

Let us now declare them −

int [][] marks;

Now, let us initialize it, wherein marks are arrays of 5 integers −

int[][] marks = new int[][]{new int[]{ 90,95 },new int[]{ 89,94 }, new int[]{ 78,87 },new int[]{ 76, 68 }, new
int[]{ 98, 91 } };

Example

Let us now see the complete example of jagged arrays in C# and learn how to implement it −

Live Demo

using System;
namespace MyApplication {
   class MyDemoClass {
      static void Main(string[] args) {
         int i, j;
         /* jagged array of 5 array of integers */
         int[][] marks = new int[][]{new int[]{90,95},new int[]{89,94}, new int[]{78,87},new int[]{ 76, 68 },
         new int[]{ 98, 91}};

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

Output

marks[0][0] = 90
marks[0][1] = 95
marks[1][0] = 89
marks[1][1] = 94
marks[2][0] = 78
marks[2][1] = 87
marks[3][0] = 76
marks[3][1] = 68
marks[4][0] = 98
marks[4][1] = 91
raja
Published on 24-Jul-2018 07:01:59
Advertisements