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

How to access elements in a rectangular array in C#?

PHPz
Release: 2023-08-30 11:57:23
forward
664 people have browsed it

如何在 C# 中访问矩形数组中的元素?

To access an element from a rectangular array, you just need to set the index of the element you want to get. Multidimensional arrays are also called rectangular arrays −

a[0,1]; // second element
Copy after login

Here is an example that shows how to use a rectangular array in C# and access an element −

Example

using System;

namespace Demo {

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

         int[,] a = new int[3, 3];
         a[0,0]= 10;
         a[0,1]= 20;
         a[0,2]= 30;
         a[1,0]= 40;
         a[1,1]= 50;
         a[1,2]= 60;
         a[2,0]= 70;
         a[2,1]= 80;

         // accessing element
         a[2,2]= 90;

         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]);
            }
         }

         int ele = a[0,1];
         Console.WriteLine("Second element: "+ele);

         Console.ReadKey();
      }
   }
}
Copy after login

The above is the detailed content of How to access elements in a rectangular 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