13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 ArraysThe assignment to array[i] in the Fill method implicitly includes a run-time check, which ensures thatthe object referenced by value is either null or an instance of a type that is compatible with the actualelement type of array. In Main, the first two invocations of Fill succeed, but the third invocation causes aSystem.ArrayTypeMismatchException to be thrown upon executing the first assignment toarray[i]. The exception occurs because a boxed int cannot be stored in a string array. end example]Array covariance specifically does not extend to arrays of value-types. For example, no conversion existsthat permits an int[] to be treated as an object[].19.6 Array initializersArray initializers may be specified in field declarations (§17.4), local variable declarations (§15.5.1), andarray creation expressions (§14.5.10.2):array-initializer:{ variable-initializer-list opt }{ variable-initializer-list , }variable-initializer-list:variable-initializervariable-initializer-list , variable-initializervariable-initializer:expressionarray-initializerAn array initializer consists of a sequence of variable initializers, enclosed by “{”and “}” tokens andseparated by “,” tokens. Each variable initializer is an expression or, in the case of a multi-dimensionalarray, a nested array initializer.The context in which an array initializer is used determines the type of the array being initialized. In an arraycreation expression, the array type immediately precedes the initializer. In a field or variable declaration, thearray type is the type of the field or variable being declared. When an array initializer is used in a field orvariable declaration, [Example: such as:int[] a = {0, 2, 4, 6, 8};end example] it is simply shorthand for an equivalent array creation expression: [Example:int[] a = new int[] {0, 2, 4, 6, 8};end example]For a single-dimensional array, the array initializer must consist of a sequence of expressions that areassignment compatible with the element type of the array. The expressions initialize array elements inincreasing order, starting with the element at index zero. The number of expressions in the array initializerdetermines the length of the array instance being created. [Example: For example, the array initializer abovecreates an int[] instance of length 5 and then initializes the instance with the following values:a[0] = 0; a[1] = 2; a[2] = 4; a[3] = 6; a[4] = 8;end example]For a multi-dimensional array, the array initializer must have as many levels of nesting as there aredimensions in the array. The outermost nesting level corresponds to the leftmost dimension and theinnermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array isdetermined by the number of elements at the corresponding nesting level in the array initializer. For eachnested array initializer, the number of elements must be the same as the other array initializers at the samelevel. [Example: 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 therightmost dimension:277

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!