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.

<strong>C#</strong> LANGUAGE SPECIFICATION19.1.1 The System.Array typeThe type System.Array is the abstract base type of all array types. An implicit reference conversion(§13.1.4) exists from any array type to System.Array, and an explicit reference conversion (§13.2.3) existsfrom System.Array to any array type. System.Array is not itself an array-type. Rather, it is a class-typefrom which all array-types are derived.At run-time, a value of type System.Array can be null or a reference to an instance of any array type.19.2 Array creationArray instances are created by array-creation-expressions (§14.5.10.2) or by field or local variabledeclarations that include an array-initializer (§19.6).When an array instance is created, the rank and length of each dimension are established and then remainconstant for the entire lifetime of the instance. In other words, it is not possible to change the rank of anexisting array instance, nor is it possible to resize its dimensions.An array instance is always of an array type. The System.Array type is an abstract type that cannot beinstantiated.Elements of arrays created by array-creation-expressions are always initialized to their default value(§12.2).19.3 Array element accessArray elements are accessed using element-access expressions (§14.5.6.1) of the form A[I 1 , I 2 , …, I N ],where A is an expression of an array type and each I X is an expression of type int, uint, long, ulong, orof a type that can be implicitly converted to one or more of these types. The result of an array element accessis a variable, namely the array element selected by the indices.The elements of an array can be enumerated using a foreach statement (§15.8.4).19.4 Array membersEvery array type inherits the members declared by the System.Array type.19.5 Array covarianceFor any two reference-types A and B, if an implicit reference conversion (§13.1.4) or explicit referenceconversion (§13.2.3) exists from A to B, then the same reference conversion also exists from the array typeA[R] to the array type B[R], where R is any given rank-specifier (but the same for both array types). Thisrelationship is known as array covariance. Array covariance, in particular, means that a value of an arraytype A[R] may actually be a reference to an instance of an array type B[R], provided an implicit referenceconversion exists from B to A.Because of array covariance, assignments to elements of reference type arrays include a run-time checkwhich ensures that the value being assigned to the array element is actually of a permitted type (§14.13.1).[Example: For example:class Test{static void Fill(object[] array, int index, int count, object value) {for (int i = index; i < index + count; i++) array[i] = value;}static void Main() {string[] strings = new string[100];Fill(strings, 0, 100, "Undefined");Fill(strings, 0, 10, null);Fill(strings, 90, 10, 0);}}276

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

Saved successfully!

Ooh no, something went wrong!