Information Technology Reference
In-Depth Information
Types of Arrays
C# provides two types of arrays:
One-dimensional arrays can be thought of as a single line, or
vector
, of elements.
Multidimensional arrays are composed such that each position in the primary vector is
itself an array, called a
sub-array
. Positions in the sub-array vectors can themselves be
sub-arrays.
In addition, there are two types of multidimensional arrays, rectangular arrays and jagged
arrays, which have the following characteristics:
Rectangular arrays
- Are multidimensional arrays where all the sub-arrays in a particular dimension have
the same length
-Always use a
single set of square brackets, regardless of the number of dimensions
int x = myArray2[4, 6, 1] // One set of square brackets
Jagged arrays
- Are multidimensional arrays where each sub-array is an independent array
- Can have sub-arrays of
different
lengths
-Use a
separate set of square brackets for each dimension of the array
jagArray1[2][7][4] // Three sets of square brackets
Figure 14-2 shows the kinds of arrays available in C#.
Figure 14-2.
One-dimensional, rectangular, and jagged arrays