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

Multi-dimensional arrays in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Multi-dimensional arrays help you store data in a table or grid form, like rows and columns. This makes it easy to organize and access related data together.

Storing a chessboard where each cell has a piece or is empty.
Representing a calendar with weeks as rows and days as columns.
Keeping track of seats in a movie theater arranged by rows and columns.
Saving pixel colors in an image with width and height dimensions.
Syntax
C Sharp (C#)
class Program
{
    static void Main()
    {
        // Declare a 2D array with 3 rows and 4 columns
        int[,] matrix = new int[3, 4];

        // Declare and initialize a 2D array
        int[,] grid = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Access element at row 1, column 2
        int value = grid[1, 2];
    }
}

Use commas inside the square brackets to separate dimensions, e.g., [rows, columns].

Multi-dimensional arrays in C# are rectangular, meaning each row has the same number of columns.

Examples
This creates an empty 2D array with zero rows and zero columns.
C Sharp (C#)
int[,] emptyArray = new int[0, 0];
A 2D array with only one element at row 0, column 0.
C Sharp (C#)
int[,] singleElementArray = new int[1, 1] { { 42 } };
Accessing the first and last elements in a 2x3 array.
C Sharp (C#)
int[,] edgeArray = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int firstElement = edgeArray[0, 0];
int lastElement = edgeArray[1, 2];
Sample Program

This program creates a 3x3 grid representing a Tic Tac Toe board. It prints the board, updates the center cell, and prints the board again to show the change.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        // Create a 3x3 multi-dimensional array
        int[,] ticTacToeBoard = new int[3, 3]
        {
            { 0, 1, 0 },
            { 1, 0, 1 },
            { 0, 0, 1 }
        };

        Console.WriteLine("Tic Tac Toe Board Before Update:");
        PrintBoard(ticTacToeBoard);

        // Update the center cell (row 1, column 1) to 1
        ticTacToeBoard[1, 1] = 1;

        Console.WriteLine("\nTic Tac Toe Board After Update:");
        PrintBoard(ticTacToeBoard);
    }

    static void PrintBoard(int[,] board)
    {
        int rows = board.GetLength(0);
        int columns = board.GetLength(1);

        for (int row = 0; row < rows; row++)
        {
            for (int column = 0; column < columns; column++)
            {
                Console.Write(board[row, column] + " ");
            }
            Console.WriteLine();
        }
    }
}
OutputSuccess
Important Notes

Accessing an element in a multi-dimensional array takes constant time O(1).

Multi-dimensional arrays use contiguous memory, so they are efficient for grid-like data.

Common mistake: Mixing up row and column indexes. Remember the first index is the row, the second is the column.

Use multi-dimensional arrays when you need a fixed-size grid. For jagged or uneven rows, consider jagged arrays instead.

Summary

Multi-dimensional arrays store data in rows and columns, like a table.

They are useful for grids, boards, and matrices.

Access elements using two indexes: [row, column].