Arrays can be thought of as long rows of slots into which values can be placed. Once you have a picture of a row of slots, imagine 10 rows, one on top of another. This is the classic two-dimensional array of rows and columns. The rows run across the array and the columns run up and down the array, as shown in Figure 10-2.
Figure 10-2. Rows and columns create a multidimensional array
A third dimension is a bit harder to imagine. Okay, now imagine four dimensions. Now imagine 10.
Those of you who are not string-theory physicists have probably given up, as have I. Multidimensional arrays are useful, however, even if you can’t quite picture what they would look like.
C# supports two types of multidimensional arrays: rectangular and jagged. In a rectangular array, every row is the same length. A jagged array, however, is an array of arrays, each of which can be a different length.
A rectangular array is an array of two (or more) dimensions. In the classic two-dimensional array, the first dimension is the number of rows and the second dimension is the number of columns.
To declare a two-dimensional array, use the following syntax:
For example, to declare and instantiate a two-dimensional
rectangular array named
myRectangularArray that contains two rows
and three columns of integers, you would write:
int [,] myRectangularArray = new ...