X hits on this document

822 views

0 shares

0 downloads

0 comments

285 / 396

C# LANGUAGE SPECIFICATION

static void Main() { string[] strings = new string[100]; Fill(strings, 0, 100, "Undefined"); Fill(strings, 0, 10, null); Fill(strings, 90, 10, 0); } }

The assignment to array[i] in the Fill method implicitly includes a run-time check which ensures that the object referenced by value is either null or an instance of a type that is compatible with the actual element type of array. In Main, the first two invocations of Fill succeed, but the third invocation causes a System.ArrayTypeMismatchException to be thrown upon executing the first assignment to array[i]. The exception occurs because a boxed int cannot be stored in a string array.

Array covariance specifically does not extend to arrays of value-types. For example, no conversion exists that permits an int[] to be treated as an object[].

12.6 Array initializers

Array initializers may be specified in field declarations (§‎10.4), local variable declarations (§‎8.5.1), and array creation expressions (§‎7.5.10.2):

array-initializer: {   variable-initializer-listopt   } {   variable-initializer-list   ,   }

variable-initializer-list: variable-initializer variable-initializer-list   ,   variable-initializer

variable-initializer: expression array-initializer

An array initializer consists of a sequence of variable initializers, enclosed by “{”and “}” tokens and separated by ,” tokens. Each variable initializer is an expression or, in the case of a multi-dimensional array, a nested array initializer.

The context in which an array initializer is used determines the type of the array being initialized. In an array creation expression, the array type immediately precedes the initializer. In a field or variable declaration, the array type is the type of the field or variable being declared. When an array initializer is used in a field or variable declaration, such as:

int[] a = {0, 2, 4, 6, 8};

it is simply shorthand for an equivalent array creation expression:

int[] a = new int[] {0, 2, 4, 6, 8};

For a single-dimensional array, the array initializer must consist of a sequence of expressions that are assignment compatible with the element type of the array. The expressions initialize array elements in increasing order, starting with the element at index zero. The number of expressions in the array initializer determines the length of the array instance being created. For example, the array initializer above creates 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;

For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number

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

Document info
Document views822
Page views822
Page last viewedSun Dec 04 09:09:40 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments