Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 14 Expressionsprevious at this levelnext at this level 14.5 Primary expressionsprevious at this levelnext at this level 14.5.10 The new operatorprevious at this levelnext at this level 14.5.10.2 Array creation expressions Paragraph 11 An array-creation-expression is used to create a new instance of an array-type. array-creation-expression : new non-array-type [ expression-list ] rank-specifiersopt array-initializeropt new array-type array-initializer Paragraph 21 An array creation expression of the first form allocates an array instance of the type that results from deleting each of the individual expressions from the expression list. 2 For example, the array creation expression new int[10,20] produces an array instance of type int[,], and the array creation expression new int[10][,] produces an array of type int[][,]. 3 Each expression in the expression list must be of type int, uint, long, or ulong, or of a type that can be implicitly converted to one or more of these types. 4 The value of each expression determines the length of the corresponding dimension in the newly allocated array instance. 5 Since the length of an array dimension must be nonnegative, it is a compile-time error to have a constant expression with a negative value, in the expression list. Paragraph 31 Except in an unsafe context (§25.1), the layout of arrays is unspecified. Paragraph 41 If an array creation expression of the first form includes an array initializer, each expression in the expression list must be a constant and the rank and dimension lengths specified by the expression list must match those of the array initializer. Paragraph 51 In an array creation expression of the second form, the rank of the specified array type must match that of the array initializer. 2 The individual dimension lengths are inferred from the number of elements in each of the corresponding nesting levels of the array initializer. 3 Thus, the expression
new int[,] {{0, 1}, {2, 3}, {4, 5}}  
exactly corresponds to
new int[3, 2] {{0, 1}, {2, 3}, {4, 5}}  
Paragraph 61 Array initializers are described further in §19.6. Paragraph 71 The result of evaluating an array creation expression is classified as a value, namely a reference to the newly allocated array instance. 2 The run-time processing of an array creation expression consists of the following steps: Paragraph 81 An array creation expression permits instantiation of an array with elements of an array type, but the elements of such an array must be manually initialized. [Example: For example, the statement
int[][] a = new int[100][];  
creates a single-dimensional array with 100 elements of type int[]. The initial value of each element is null. end example]
2 It is not possible for the same array creation expression to also instantiate the sub-arrays, and the statement
int[][] a = new int[100][5];    // Error  
results in a compile-time error.
3 Instantiation of the sub-arrays must instead be performed manually, as in
int[][] a = new int[100][];  
for (int i = 0; i < 100; i++) a[i] = new int[5];  
Paragraph 91 When an array of arrays has a "rectangular" shape, that is when the sub-arrays are all of the same length, it is more efficient to use a multi-dimensional array. 2 In the example above, instantiation of the array of arrays creates 101 objects-one outer array and 100 sub-arrays. 3 In contrast,
int[,] = new int[100, 5];  
creates only a single object, a two-dimensional array, and accomplishes the allocation in a single statement.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS