Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to access elements from a rectangular array in C#?
To access elements from a rectangular array in C#, you need to specify the row and column indices using square brackets. Multi-dimensional arrays are also called rectangular arrays because they form a rectangular structure with fixed dimensions.
Syntax
Following is the syntax for accessing elements in a rectangular array −
dataType[,] arrayName = new dataType[rows, columns]; arrayName[rowIndex, columnIndex] = value;
To retrieve an element −
dataType element = arrayName[rowIndex, columnIndex];
Using Basic Element Access
Example
using System;
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;
a[2,2] = 90;
// Accessing specific elements
int element1 = a[0,1]; // Second element in first row
int element2 = a[2,2]; // Last element
Console.WriteLine("Element at [0,1]: " + element1);
Console.WriteLine("Element at [2,2]: " + element2);
// Display all elements
Console.WriteLine("\nAll elements:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
}
}
}
The output of the above code is −
Element at [0,1]: 20 Element at [2,2]: 90 All elements: 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 a[2,2] = 90
Using Array Initialization
Example
using System;
class Program {
static void Main(string[] args) {
// Initialize array with values directly
int[,] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Access diagonal elements
Console.WriteLine("Diagonal elements:");
for (int i = 0; i < 3; i++) {
Console.WriteLine("numbers[{0},{1}] = {2}", i, i, numbers[i,i]);
}
// Access elements in second column
Console.WriteLine("\nSecond column elements:");
for (int i = 0; i < 3; i++) {
Console.WriteLine("numbers[{0},{1}] = {2}", i, 1, numbers[i,1]);
}
}
}
The output of the above code is −
Diagonal elements: numbers[0,0] = 1 numbers[1,1] = 5 numbers[2,2] = 9 Second column elements: numbers[0,1] = 2 numbers[1,1] = 5 numbers[2,1] = 8
Common Access Patterns
| Pattern | Example | Description |
|---|---|---|
| Single Element | array[1,2] |
Access element at row 1, column 2 |
| Entire Row |
array[0,j] in loop |
Access all elements in row 0 |
| Entire Column |
array[i,1] in loop |
Access all elements in column 1 |
| Diagonal |
array[i,i] in loop |
Access diagonal elements |
Conclusion
Accessing elements in rectangular arrays requires specifying both row and column indices using the format array[row, column]. You can access individual elements, entire rows, columns, or follow specific patterns like diagonals using appropriate loop structures.
Advertisements
