Information Technology Reference
In-Depth Information
For example, read the following declaration as “ intArray has four groups of three groups
of two elements.”
Initialization lists, nested and separated by commas
int[,,] intArray = new int[4,3,2] {
{ {8, 6}, {5, 2}, {12, 9} },
{ {6, 4}, {13, 9}, {18, 4} },
{ {7, 2}, {1, 13}, {9, 3} },
{ {4, 6}, {3, 2}, {23, 8} }
};
Shortcut Syntax
When combining declaration, array creation, and initialization in a single statement, the
array creation expression part of the syntax can be left out. This shortcut syntax is shown in
Figure 14-9.
Figure 14-9. Shortcut for array declaration, creation, and initialization
Putting It All Together
Now that you have all the pieces, they can be put together in a full example. The following code
creates, initializes, and uses a rectangular array.
// Declare, create, and initialize the array.
int[,] arr = new int[2,3] {{0, 1, 2}, {10, 11, 12}};
// Print the values.
for( int i=0; i<2; i++ )
for( int j=0; j<3; j++ )
Console.WriteLine("Element [{0},{1}] is {2}", i, j, arr[i,j]);
This code produces the following output:
Element [0,0] is 0
Element [0,1] is 1
Element [0,2] is 2
Element [1,0] is 10
Element [1,1] is 11
Element [1,2] is 12
Search WWH ::




Custom Search