DEV Community

Sabin Sim
Sabin Sim

Posted on

28. C# (Multidimensional Arrays)

using System;

class Program
{
    static void Main()
    {
        char[,] board =
        {
            { 'A', 'B', 'C' },
            { 'D', 'E', 'F' }
        };

        Console.WriteLine("Row count: " + board.GetLength(0));
        Console.WriteLine("Column count: " + board.GetLength(1));
        Console.WriteLine("Total elements (Length): " + board.Length);
        Console.WriteLine();

        for (int i = 0; i < board.GetLength(0); i++)
        {
            for (int j = 0; j < board.GetLength(1); j++)
            {
                Console.WriteLine($"i={i}, j={j}, value={board[i, j]}");
            }
        }

        Console.ReadKey();
    }
}
Enter fullscreen mode Exit fullscreen mode

0. The Real Goal of This Lesson

A multidimensional array is one structure with multiple dimensions.
Each dimension has its own size.

If you misunderstand this:

  • You misuse Length
  • You write incorrect loops
  • You trigger runtime index errors

This lesson is about structural clarity.


1. Understanding the Array Structure

char[,] board =
{
    { 'A', 'B', 'C' },
    { 'D', 'E', 'F' }
};
Enter fullscreen mode Exit fullscreen mode

This is a 2D array with:

  • 2 rows
  • 3 columns

Visual representation:

(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
Enter fullscreen mode Exit fullscreen mode

Important distinction:

  • board.Length → 6 (total elements)
  • board.GetLength(0) → 2 (rows)
  • board.GetLength(1) → 3 (columns)

Length is not the row count.


2. Why Two Loops Are Required

A 1D array uses one index:

numbers[i]
Enter fullscreen mode Exit fullscreen mode

A 2D array requires two:

board[i, j]
Enter fullscreen mode Exit fullscreen mode

Meaning:

  • i moves across rows
  • j moves across columns

Execution pattern:

Outer loop = row set
Inner loop = full column traversal per row

Nested loops are not optional.
They are structurally required.


3. Step-by-Step Execution Flow

First row (i = 0)

  • j = 0 → A
  • j = 1 → B
  • j = 2 → C

Inner loop ends.


Second row (i = 1)

  • j = 0 → D
  • j = 1 → E
  • j = 2 → F

Termination

i becomes 2 → outer condition fails → traversal stops.

Execution count:

rows × columns = 2 × 3 = 6
Enter fullscreen mode Exit fullscreen mode

4. Why Using Length Alone Is Dangerous

Incorrect loop:

for (int i = 0; i < board.Length; i++)
Enter fullscreen mode Exit fullscreen mode

board.Length = 6

But valid row indices:

0 and 1 only.

When i = 2, you effectively access:

board[2, 0]
Enter fullscreen mode Exit fullscreen mode

Runtime error.

Length represents total elements, not row range.


Learning Check (One Question)

Given:

int[,] matrix = new int[3, 4];
Enter fullscreen mode Exit fullscreen mode

Answer all of these:

  1. What is matrix.Length?
  2. What is matrix.GetLength(0)?
  3. What is matrix.GetLength(1)?
  4. How many total iterations will the nested loops run?

Compute the execution count precisely.

Final Summary

When dimension count increases, index count increases.
Loop structure must expand accordingly.

Top comments (0)