C# Multidimensional Array

In C#, a multidimensional array is an array that contains more than one dimension. Multidimensional arrays are useful for representing data in a matrix-like format.

In C#, a multidimensional array is declared using the following syntax:

re‮ot ref‬:theitroad.com
type[,] arrayName = new type[size1, size2];

In this syntax, type is the data type of the array elements, arrayName is the name of the array, and size1 and size2 are the sizes of the first and second dimensions, respectively. You can specify as many dimensions as you need by adding additional comma-separated sizes.

Here's an example of how to declare and initialize a two-dimensional array in C#:

int[,] matrix = new int[3, 4];

In this example, we declare a two-dimensional integer array named matrix with three rows and four columns.

We can also initialize the multidimensional array using an array initializer:

int[,] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

In this example, we initialize the matrix array with three rows and four columns, where the first row is {1, 2, 3, 4}, the second row is {5, 6, 7, 8}, and the third row is {9, 10, 11, 12}.

To access the elements of a multidimensional array, you need to specify the indices of each dimension. For example, to access the element in the second row and third column of the matrix array, you would use the following code:

int element = matrix[1, 2]; // row 1, column 2

In this code, matrix[1, 2] returns the value 7, which is the element in the second row and third column of the matrix array.