Information Technology Reference
In-Depth Information
Figure 14-7.
Explicit initialization of a one-dimensional array
Explicit Initialization of Rectangular Arrays
To explicitly initialize a rectangular array:
Each
vector of initial values
must be enclosed in curly braces.
Each
dimension
must also be nested and enclosed in curly braces.
In addition to the initial values, the initialization lists and components of each dimen-
sion must also be separated by commas.
For example, the following code shows the declaration of a two-dimensional array with an
initialization list. Figure 14-8 illustrates the layout in memory.
Initialization lists separated by commas
↓
↓
int[,] intArray2 = new int[3,2] { {10, 1}, {2, 10}, {11, 9} } ;
Figure 14-8.
Initializing a rectangular array
Syntax Points for Initializing Rectangular Arrays
Rectangular arrays are initialized with nested, comma-separated initialization lists. The initial-
ization lists are nested in curly braces. This can sometimes be confusing, so to get the nesting,
grouping, and commas right, the following tips can be helpful:
Commas are used as
separators
between all
elements
and
groups
.
Commas are
not
placed between left curly braces.
Commas are
not
placed before a right curly brace.
Read the rank specifications from left to right, designating the last number as “elements”
and all the others as “groups.”