Curriculum
In addition to single-dimensional arrays, C# also supports multi-dimensional arrays, which are used to store data in a grid or matrix format. A multi-dimensional array can be two-dimensional, three-dimensional, or higher, and each dimension is specified using a separate set of square brackets in the array declaration.
Here is an example of declaring and initializing a two-dimensional integer array:
int[,] grid = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
This creates a two-dimensional integer array called grid
with three rows and four columns, and initializes its values with a nested set of braces. The first row contains the values 1, 2, 3, and 4, the second row contains 5, 6, 7, and 8, and the third row contains 9, 10, 11, and 12.
You can access the elements of a multi-dimensional array using their indexes, which are specified using a separate set of square brackets for each dimension. Here is an example of accessing an element in the grid
array:
int element = grid[1, 2]; // this retrieves the value 7
This retrieves the value in the second row and third column of the grid
array, which is 7.
Multi-dimensional arrays can be used in a wide range of applications, such as image processing, scientific simulations, and game development. They provide a flexible way to store and manipulate data in a structured format, and are an essential tool in many programming tasks.