X hits on this document

825 views

0 shares

0 downloads

0 comments

286 / 396

Chapter ‎18   Unsafe code

of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level. The example:

int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};

creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the rightmost dimension:

int[,] b = new int[5, 2];

and then initializes the array instance with the following values:

b[0, 0] = 0; b[0, 1] = 1; b[1, 0] = 2; b[1, 1] = 3; b[2, 0] = 4; b[2, 1] = 5; b[3, 0] = 6; b[3, 1] = 7; b[4, 0] = 8; b[4, 1] = 9;

When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length. Here are some examples:

int i = 3; int[] x = new int[3] {0, 1, 2};// OK int[] y = new int[i] {0, 1, 2};// Error, i not a constant int[] z = new int[3] {0, 1, 2, 3};// Error, length/initializer mismatch

Here, the initializer for y results in a compile-time error because the dimension length expression is not a constant, and the initializer for z results in a compile-time error because the length and the number of elements in the initializer do not agree.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.273

Document info
Document views825
Page views825
Page last viewedSun Dec 04 12:12:06 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments