Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Multi-dimensional arrays in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multi-dimensional arrays
Declare multi-dimensional array
↓
Allocate memory for rows and columns
↓
Access elements using two indices
↓
Modify or read element at [row, column
↓
Use nested loops to traverse all elements
↓
Done
This flow shows how a multi-dimensional array is declared, allocated, accessed by row and column indices, and traversed using nested loops.
Execution Sample
C Sharp (C#)
int[,] matrix = new int[2,3];
matrix[0,0] = 5;
matrix[1,2] = 10;
for(int i=0; i<2; i++) {
  for(int j=0; j<3; j++) {
    Console.Write(matrix[i,j] + " ");
  }
  Console.WriteLine();
}
This code creates a 2x3 integer array, sets two values, and prints all elements row by row.
Execution Table
StepActionIndices (i,j)Value Set/ReadOutputNotes
1Declare array---Create 2 rows and 3 columns, all initialized to 0
2Set value[0,0]5-Set element at first row, first column to 5
3Set value[1,2]10-Set element at second row, third column to 10
4Start outer loopi=0--Begin first row traversal
5Start inner loopi=0, j=0-5 Read matrix[0,0] = 5 and print
6Inner loopi=0, j=1-0 Default value 0 printed
7Inner loopi=0, j=2-0 Default value 0 printed
8End inner loopi=0-Line breakMove to next line after row 0
9Outer loopi=1--Begin second row traversal
10Inner loopi=1, j=0-0 Default value 0 printed
11Inner loopi=1, j=1-0 Default value 0 printed
12Inner loopi=1, j=2-10 Read matrix[1,2] = 10 and print
13End inner loopi=1-Line breakMove to next line after row 1
14End outer loop---All elements printed, loops end
💡 Loops finish after i=1 and j=2, all elements accessed and printed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 12Final
matrix[0,0]055555
matrix[1,2]0010101010
i---01-
j---0 to 20 to 2-
Key Moments - 3 Insights
Why do we use two indices like matrix[i,j] instead of one?
Because the array has rows and columns, each index selects the row and column respectively, as shown in execution_table steps 5 and 12 where both i and j are used to access elements.
Why are some elements zero even though we only set two values?
In C#, multi-dimensional arrays of int are initialized with zeros by default, so elements not explicitly set remain zero, as seen in steps 6,7,10,11.
How do nested loops help in accessing all elements?
The outer loop selects the row (i), and the inner loop selects the column (j), together covering every element, demonstrated in execution_table steps 4-13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what value is read from matrix[0,0]?
A10
B5
C0
DUndefined
💡 Hint
Check the 'Value Set/Read' and 'Output' columns at step 5 in execution_table.
At which step does the inner loop finish printing the first row?
AStep 8
BStep 7
CStep 13
DStep 14
💡 Hint
Look for the 'End inner loop' action with a line break after row 0 in execution_table.
If we change matrix[1,2] = 10 to matrix[1,2] = 20, what changes in the output at step 12?
AOutput remains 10
BOutput changes to 0
COutput changes from 10 to 20
DNo output at step 12
💡 Hint
Check the 'Value Set/Read' and 'Output' columns at step 12 in execution_table.
Concept Snapshot
Multi-dimensional arrays in C# use syntax: type[,] name = new type[rows, columns];
Access elements with two indices: array[row, column].
Elements default to zero for int arrays.
Use nested loops to traverse rows and columns.
Each index selects a dimension (row or column).
Useful for tables, grids, and matrices.
Full Transcript
This lesson shows how to work with multi-dimensional arrays in C#. We start by declaring an array with two dimensions, specifying rows and columns. Memory is allocated for all elements, which are initialized to zero by default for integers. We set specific elements using two indices, one for the row and one for the column. To read or print all elements, we use nested loops: the outer loop goes through rows, the inner loop through columns. Each element is accessed by matrix[i,j]. The execution table traces each step, showing how values are set and read, and how output is produced line by line. Key moments clarify why two indices are needed, why some elements remain zero, and how nested loops cover all elements. The visual quiz tests understanding of specific steps and output changes. The snapshot summarizes syntax and usage for quick reference.

Practice

(1/5)
1. What is a multi-dimensional array in C#?
easy
A. An array that can only hold one value
B. An array that stores only strings
C. An array with more than one dimension, like rows and columns
D. A list of unrelated variables

Solution

  1. Step 1: Understand array dimensions

    A multi-dimensional array stores data in multiple dimensions, such as rows and columns, unlike a simple one-dimensional array.
  2. Step 2: Recognize the definition

    An array with more than one dimension, like rows and columns correctly describes a multi-dimensional array as having more than one dimension, like a table with rows and columns.
  3. Final Answer:

    An array with more than one dimension, like rows and columns -> Option C
  4. Quick Check:

    Multi-dimensional array = rows and columns [OK]
Hint: Think of a table with rows and columns [OK]
Common Mistakes:
  • Confusing multi-dimensional arrays with simple lists
  • Thinking arrays can only hold one type of data
  • Believing multi-dimensional arrays are unrelated variables
2. Which of the following is the correct way to declare a 2x3 multi-dimensional array of integers in C#?
easy
A. int[,] arr = new int[2,3];
B. int[][] arr = new int[2][3];
C. int arr[2,3];
D. int arr = new int[2,3];

Solution

  1. Step 1: Recall C# multi-dimensional array syntax

    In C#, a 2D array is declared with commas inside square brackets: int[,] arr = new int[rows, columns];
  2. Step 2: Check each option

    int[,] arr = new int[2,3]; uses correct syntax. int[][] arr = new int[2][3]; is jagged array syntax, not multi-dimensional. Options A and C are invalid syntax.
  3. Final Answer:

    int[,] arr = new int[2,3]; -> Option A
  4. Quick Check:

    Use comma inside brackets for multi-dimensional arrays [OK]
Hint: Use comma inside brackets for multi-dimensional arrays [OK]
Common Mistakes:
  • Using jagged array syntax instead of multi-dimensional
  • Missing 'new' keyword or wrong brackets
  • Confusing array declaration with other languages
3. What will be the output of the following C# code?
int[,] matrix = { {1, 2, 3}, {4, 5, 6} };
Console.WriteLine(matrix[1, 2]);
medium
A. 3
B. 5
C. Runtime error
D. 6

Solution

  1. Step 1: Understand array indexing

    matrix[1, 2] means row 1, column 2. Rows and columns start at 0.
  2. Step 2: Locate the value at matrix[1, 2]

    Row 1 is {4, 5, 6}, column 2 is the third element, which is 6.
  3. Final Answer:

    6 -> Option D
  4. Quick Check:

    matrix[1,2] = 6 [OK]
Hint: Remember indexing starts at zero for rows and columns [OK]
Common Mistakes:
  • Confusing row and column indexes
  • Starting count from 1 instead of 0
  • Expecting the value 3 or 5 instead of 6
4. Identify the error in this C# code snippet:
int[,] grid = new int[3, 3];
grid[3, 0] = 10;
medium
A. Index out of range error at grid[3, 0]
B. Syntax error in array declaration
C. Missing initialization of array
D. No error, code runs fine

Solution

  1. Step 1: Check array size and valid indexes

    The array grid has size 3x3, so valid row indexes are 0, 1, 2.
  2. Step 2: Analyze the assignment grid[3, 0]

    Index 3 for row is out of range, causing an IndexOutOfRangeException at runtime.
  3. Final Answer:

    Index out of range error at grid[3, 0] -> Option A
  4. Quick Check:

    Index 3 invalid for size 3 array [OK]
Hint: Indexes start at 0, max index is size-1 [OK]
Common Mistakes:
  • Using index equal to array size
  • Confusing syntax error with runtime error
  • Assuming arrays auto-expand on invalid index
5. Given a 3x3 multi-dimensional array int[,] board representing a tic-tac-toe game, which code snippet correctly counts how many cells contain the value 1?
int count = 0;
// Which loop is correct?
A) for (int i = 0; i < 3; i++)
   for (int j = 0; j < 3; j++)
       if (board[i, j] == 1) count++;

B) for (int i = 1; i <= 3; i++)
   for (int j = 1; j <= 3; j++)
       if (board[i, j] == 1) count++;

C) for (int i = 0; i < 3; i++)
   for (int j = 0; j < 2; j++)
       if (board[i, j] == 1) count++;

D) for (int i = 0; i < 3; i++)
   for (int j = 0; j < 3; j++)
       if (board[j, i] == 1) count++;
hard
A. rorre egnar fo tuo xedni sesuaC
B. Causes index out of range error
C. Misses last column in counting
D. Counts transposed cells, not original positions

Solution

  1. Step 1: Understand array indexing and loop ranges

    Indexes start at 0 and go up to size-1, so loops must run from 0 to less than 3.
  2. Step 2: Analyze each option's loops

    Causes index out of range error correctly counts all cells with value 1 loops correctly over all rows and columns. Causes index out of range error causes index out of range error uses 1 to 3, causing out of range. Misses last column in counting misses last column by looping j to 2. Counts transposed cells, not original positions swaps indexes, counting transposed cells.
  3. Final Answer:

    Causes index out of range error -> Option B
  4. Quick Check:

    Loop from 0 to less than size for rows and columns [OK]
Hint: Loop indexes from 0 to size-1 for all dimensions [OK]
Common Mistakes:
  • Starting loops at 1 instead of 0
  • Using wrong loop limits causing missed elements
  • Swapping row and column indexes accidentally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard