C-SHARP - 4.1 Advanced tables

This video dives deeper into arrays in C#. A reminder: an array holds a fixed number of values of the same type, all referenced by an index that starts at 0. We start by creating two single-dimension arrays: one int[] initialised between braces and one string[] initialised cell by cell using the index. So far we have only used one-dimensional arrays, which represent a single row of columns. C# also lets us build multidimensional arrays, where each element can itself contain several values: the most common is a two-dimensional array with rows and columns, perfect for representing a matrix or a grid.

To declare a 2D array we type var tab = new string[3, 3]: the first number is the row count, the second the column count. To initialise it inline we open braces, then for each row we open another pair of braces with the values in order. Accessing a value now requires two indexes: tab[row, column]. With tab[1, 2] we ask for the value at row 1, column 2.

var tab = new string[3, 3]
{
    { "Jason", "Tibo", "Tom" },
    { "Marc",  "Paul", "Anna" },
    { "Lou",   "Eli",  "Sam"  }
};

for (int i = 0; i < 3; i++)
{
    Console.WriteLine(string.Format("Group {0}", i + 1));
    for (int j = 0; j < 3; j++)
    {
        Console.WriteLine(tab[i, j]);
    }
}

Nested loops

  • The outer loop iterates over the rows (variable i).
  • The inner loop iterates over the columns of the current row (variable j).
  • Inside the inner loop we read tab[i, j] to get every value of the matrix.

To display all the names we use nested loops: a first for walks through the rows, and inside it a second for walks through the columns. At each iteration of the inner loop we print tab[i, j]. Imagine a character climbing 10 stairs to reach a floor and 5 floors in total: the outer loop counts the floors, the inner loop counts the stairs of the current floor. That is exactly the spirit of nested loops on a 2D array.

Summary

This lesson introduces advanced array concepts in C#, covering both one-dimensional and multidimensional arrays. It demonstrates how to declare, initialize, and access array elements using proper indexing syntax. The lesson reinforces the fundamentals—arrays store a fixed number of variables of the same type—before progressing to 2D arrays with practical examples like storing student names organized by classroom groups.

Key points

  • Arrays in C# store a fixed number of variables of the same type (int, string, etc.)
  • One-dimensional arrays contain a single row with multiple columns; multidimensional arrays have rows and columns
  • Arrays are initialized either with values in braces {1, 2, 3} or by assigning individual elements using indexes
  • Elements are accessed using indexes starting from 0; 2D arrays require two indexes: array[row, column]
  • The 'var' keyword allows declaring variables without explicit type, as the compiler infers the type from initialization
  • 2D arrays effectively represent real-world data structures like classroom grids (rows = groups, columns = students)

FAQ

What is the difference between one-dimensional and multidimensional arrays?

One-dimensional arrays contain a single row with multiple columns (like a list). Multidimensional arrays (2D, 3D) contain multiple rows and columns, allowing data to be organized in grids or matrices.

How do you access a specific element in a 2D array?

Use two indexes in square brackets: arrayName[row, column]. For example, classArray[1, 2] accesses the element in the second row and third column, with indexes starting from 0.

What are the two ways to initialize an array in C#?

Immediate initialization uses braces with values: int[] numbers = new int[3] {1, 2, 3}. Separate initialization creates the array first and assigns values individually: numbers[0] = 1; numbers[1] = 2.