Home  >  Article  >  Backend Development  >  How to access elements in multidimensional array in C#?

How to access elements in multidimensional array in C#?

PHPz
PHPzforward
2023-09-06 14:29:051057browse

How to access elements in multidimensional array in C#?

To access an element in a multidimensional array, just add the index of the desired element, for example -

a[2,1]

The above accesses the element from row 3, column 2, i.e. Element 3 as shown below in out[3,4] array -

0 0
1 2
2 4
3 6

Let’s see what we discussed and access elements in 2D array-

Example

using System;

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

         int[,] a = new int[4, 2] {{0,0}, {1,2}, {2,4}, {3,6} };
         int i, j;

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

         }

         // accessing element
         Console.WriteLine(a[2,1]);

         Console.ReadKey();
      }
   }
}

The above is the detailed content of How to access elements in multidimensional array in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete