Light Mode Image

C# Multidimensional Array

A multidimensional array has more than one dimension. The most common multidimensional array is a two-dimensional array, but C# also supports arrays with three or more dimensions.

 

1. Two-Dimensional Array

 

A two-dimensional array is a type of array that can store elements in a grid of rows and columns. It is essentially an array of arrays. 

 

Example of an two-dimensional array in C#.

using System;

class Program
{
    static void Main()
    {
        // Declare and initialize a 2D array
        int[,] twoDArray = new int[3, 4];

        // Populate the array
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                twoDArray[i, j] = i * 4 + j + 1;
            }
        }

        // Access and print elements of the array
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Console.Write(twoDArray[i, j] + "\t");
            }
            Console.WriteLine();
        }
    }
}

 

In this example, twoDArray is a 3x4 array (3 rows and 4 columns). You can change the size of the array by modifying the dimensions in the declaration (new int[3, 4]).

 

You can also initialize a 2D array with specific values.

int[,] initializedArray = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

 

Accessing elements of a 2D array is done using two indices, one for the row and one for the column (array[row, column]). The indices for arrays in C# are zero-based, so the first row or column is accessed with index 0.

 

2. Three-Dimensional Array

 

You can create a three-dimensional array using the following syntax.

 

data_type[,,] array_name = new data_type[size1, size2, size3];

Here, data_type is the type of elements that the array will hold (e.g., intdoublestring), and size1size2, and size3 are the sizes of the three dimensions of the array.

 

Example of an three-dimensional array in C#.

using System;

class Program
{
    static void Main()
    {
        // Creating a 3D array of integers with sizes 2x3x4
        int[,,] threeDArray = new int[2, 3, 4];

        // Assigning values to the elements of the array
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    threeDArray[i, j, k] = i + j + k;
                }
            }
        }

        // Accessing and printing the elements of the array
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    Console.WriteLine($"threeDArray[{i}, {j}, {k}] = {threeDArray[i, j, k]}");
                }
            }
        }
    }
}

 

In this example, the three-dimensional array is of type int and has dimensions 2x3x4. The program then assigns values to each element of the array and prints the values.