0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.