Multidimensional Arrays

If we simplify the mailbox analogy from earlier in the chapter, you can think of an array as a long row of slots into which you can place values. This single row of slots is called a one-dimensional array. Now imagine 10 rows of slots, stacked on top of each other. What you’re picturing is a 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.

Rows and columns create a multidimensional array.

Figure 10-2. Rows and columns create a multidimensional array.

Two-dimensional arrays aren’t too hard to picture, but a third dimension is a bit harder to imagine, but still not too hard, if you remember your high school geometry. OK, now imagine four dimensions. Now imagine 10.

Those of you who are not string-theory physicists have probably given up, as have we. Maybe we can’t picture what 10-dimensional arrays look like, but that doesn’t matter to C#. Multidimensional arrays can be useful, even if you can’t quite picture them.

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.

Rectangular Arrays

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 ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.