X hits on this document





284 / 396

Chapter ‎18   Unsafe code

At run-time, a value of an array type can be null or a reference to an instance of that array type.

12.1.1 The System.Array type

The type System.Array is the abstract base type of all array types. An implicit reference conversion (§‎6.1.4) exists from any array type to System.Array, and an explicit reference conversion (§‎6.2.3) exists from System.Array to any array type. Note that System.Array is not itself an array-type. Rather, it is a class-type from 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.

12.2 Array creation

Array instances are created by array-creation-expressions (§‎ or by field or local variable declarations that include an array-initializer (§‎12.6).

When an array instance is created, the rank and length of each dimension are established and then remain constant for the entire lifetime of the instance. In other words, it is not possible to change the rank of an existing 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 be instantiated.

Elements of arrays created by array-creation-expressions are always initialized to their default value (§‎5.2).

12.3 Array element access

Array elements are accessed using element-access expressions (§‎ of the form A[I1, I2, ..., IN], where A is an expression of an array type and each IX is an expression of type int, uint, long, ulong, or of a type that can be implicitly converted to one or more of these types. The result of an array element access is a variable, namely the array element selected by the indices.

The elements of an array can be enumerated using a foreach statement (§‎8.8.4).

12.4 Array members

Every array type inherits the members declared by the System.Array type.

12.5 Array covariance

For any two reference-types A and B, if an implicit reference conversion (§‎6.1.4) or explicit reference conversion (§‎6.2.3) exists from A to B, then the same reference conversion also exists from the array type A[R] to the array type B[R], where R is any given rank-specifier (but the same for both array types). This relationship is known as array covariance. Array covariance in particular means that a value of an array type A[R] may actually be a reference to an instance of an array type B[R], provided an implicit reference conversion exists from B to A.

Because of array covariance, assignments to elements of reference type arrays include a run-time check which ensures that the value being assigned to the array element is actually of a permitted type (§‎7.13.1). 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; }

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

Document info
Document views1248
Page views1248
Page last viewedFri Jan 20 04:16:22 UTC 2017