Home > Backend Development > C#.Net Tutorial > How to access elements in multidimensional array in C#?

How to access elements in multidimensional array in C#?

PHPz
Release: 2023-09-06 14:29:05
forward
1099 people have browsed it

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]
Copy after login

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
Copy after login

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();
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template