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
How do we access elements from the two-dimensional array in C#?
A two-dimensional array in C# can be thought of as a table structure with rows and columns. Elements are accessed using two indices: the row index and the column index, separated by a comma within square brackets.
Syntax
Following is the syntax for accessing elements from a two-dimensional array −
dataType value = arrayName[rowIndex, columnIndex];
Following is the syntax for assigning values to elements −
arrayName[rowIndex, columnIndex] = value;
Accessing Individual Elements
Example
using System;
class Program {
static void Main(string[] args) {
// Declare and initialize 2D array
int[,] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Access specific elements
Console.WriteLine("Element at [0,0]: " + numbers[0,0]);
Console.WriteLine("Element at [1,2]: " + numbers[1,2]);
Console.WriteLine("Element at [2,1]: " + numbers[2,1]);
// Modify an element
numbers[1,1] = 50;
Console.WriteLine("Modified element at [1,1]: " + numbers[1,1]);
}
}
The output of the above code is −
Element at [0,0]: 1 Element at [1,2]: 6 Element at [2,1]: 8 Modified element at [1,1]: 50
Accessing All Elements Using Loops
Example
using System;
class MyArray {
static void Main(string[] args) {
/* an array with 5 rows and 2 columns*/
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
}
// Access a specific element
int x = a[1,1];
Console.WriteLine("Specific element a[1,1] = " + x);
}
}
The output of the above code is −
a[0,0] = 0 a[0,1] = 0 a[1,0] = 1 a[1,1] = 2 a[2,0] = 2 a[2,1] = 4 a[3,0] = 3 a[3,1] = 6 a[4,0] = 4 a[4,1] = 8 Specific element a[1,1] = 2
Using GetLength() Method
The GetLength() method returns the number of elements in a specific dimension, making loops more dynamic −
Example
using System;
class Program {
static void Main(string[] args) {
int[,] matrix = { {10, 20}, {30, 40}, {50, 60} };
Console.WriteLine("Array dimensions: {0} rows, {1} columns",
matrix.GetLength(0), matrix.GetLength(1));
// Use GetLength() in loops
for (int i = 0; i < matrix.GetLength(0); i++) {
for (int j = 0; j < matrix.GetLength(1); j++) {
Console.WriteLine("matrix[{0},{1}] = {2}", i, j, matrix[i,j]);
}
}
}
}
The output of the above code is −
Array dimensions: 3 rows, 2 columns matrix[0,0] = 10 matrix[0,1] = 20 matrix[1,0] = 30 matrix[1,1] = 40 matrix[2,0] = 50 matrix[2,1] = 60
Conclusion
Two-dimensional arrays in C# are accessed using row and column indices in the format array[row, column]. You can access individual elements directly or iterate through all elements using nested loops. The GetLength() method helps determine the dimensions dynamically for more flexible code.
