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
C# program to Loop over a two dimensional array
A two-dimensional array in C# stores elements in a grid format with rows and columns. To loop through all elements, you need to iterate through both dimensions using nested loops.
Syntax
Following is the syntax for declaring a two-dimensional array −
dataType[,] arrayName = new dataType[rows, columns];
Following is the syntax for looping through a two-dimensional array using nested for loops −
for (int i = 0; i <= array.GetUpperBound(0); i++) {
for (int j = 0; j <= array.GetUpperBound(1); j++) {
// Access array[i, j]
}
}
Using GetUpperBound() Method
The GetUpperBound() method returns the highest index of a specified dimension. For a 3x3 array, GetUpperBound(0) returns 2 (rows) and GetUpperBound(1) returns 2 (columns).
Example
using System;
public class Demo {
public static void Main() {
string[,] array = new string[3, 3];
array[0, 0] = "One";
array[0, 1] = "Two";
array[0, 2] = "Three";
array[1, 0] = "Four";
array[1, 1] = "Five";
array[1, 2] = "Six";
array[2, 0] = "Seven";
array[2, 1] = "Eight";
array[2, 2] = "Nine";
// getting upper bound
int uBound0 = array.GetUpperBound(0);
int uBound1 = array.GetUpperBound(1);
for (int i = 0; i <= uBound0; i++) {
for (int j = 0; j <= uBound1; j++) {
string res = array[i, j];
Console.WriteLine(res);
}
}
}
}
The output of the above code is −
One Two Three Four Five Six Seven Eight Nine
Using Array Length Property
You can also use the Length property with GetLength() method to determine the size of each dimension −
Example
using System;
public class Demo {
public static void Main() {
int[,] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Console.WriteLine("Array dimensions: " + numbers.GetLength(0) + "x" + numbers.GetLength(1));
for (int i = 0; i < numbers.GetLength(0); i++) {
for (int j = 0; j < numbers.GetLength(1); j++) {
Console.Write(numbers[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Array dimensions: 3x3 1 2 3 4 5 6 7 8 9
Using foreach Loop
The foreach loop provides a simpler way to iterate through all elements without managing indices −
Example
using System;
public class Demo {
public static void Main() {
string[,] fruits = {{"Apple", "Banana"}, {"Orange", "Grape"}, {"Mango", "Cherry"}};
Console.WriteLine("Using foreach loop:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Using foreach loop: Apple Banana Orange Grape Mango Cherry
Comparison of Loop Methods
| Method | Advantages | Use Case |
|---|---|---|
| GetUpperBound() | Works with any array size, provides index access | When you need both index and element |
| GetLength() | More intuitive, easier to read | When you prefer standard loop syntax |
| foreach | Simplest syntax, no index management | When you only need element values |
Conclusion
Looping over two-dimensional arrays in C# can be done using nested for loops with GetUpperBound(), GetLength(), or the simpler foreach loop. Choose the method based on whether you need index access or just the element values.
