Unlike the jagged array (string[][]) from your previous example, a true rectangular 2D array in C# uses a comma inside the square brackets (string[,]).

In a rectangular 2D array, every row has the exact same fixed length, forming a strict grid or matrix.

Example: Rectangular 2D Array ([,])

// Declaration and initialization of a 3x2 rectangular 2D array
string[,] matrix = new string[,]
{
    { "Round", "Apple" },
    { "Long", "Banana" },
    { "Green", "Watermelon" }
};
 
// 1. Accessing a specific element by its coordinates [row, column]
Console.WriteLine(matrix[0, 1]); // Output: Apple (Row 0, Column 1)
 
// 2. Iterating through the array using a standard foreach loop 
// (A foreach loop treats a 2D array as a single flattened sequence)
Console.WriteLine("\n--- Flattened Iteration ---");
foreach (string item in matrix)
{
    Console.WriteLine(item);
}
 
// 3. Iterating using row and column indices (Grid style)
// GetLength(0) gives the number of rows, GetLength(1) gives the number of columns
Console.WriteLine("\n--- Grid Iteration ---");
for (int row = 0; row < matrix.GetLength(0); row++)
{
    for (int col = 0; col < matrix.GetLength(1); col++)
    {
        Console.Write($"{matrix[row, col]} \t");
    }
    Console.WriteLine(); // New line after each row
}
 

Key Takeaway: [,] vs [][]

  • string[,] (Rectangular 2D Array): Forms a rigid grid. Every inner row has the same number of columns.
  • string[][] (Jagged Array / Array of Arrays): Each row is an independent array, meaning one row can have 2 elements and the next can have 5 elements.