Language Specification Version 0 Notice



Download 3.2 Mb.
Page64/85
Date29.01.2017
Size3.2 Mb.
#10878
1   ...   60   61   62   63   64   65   66   67   ...   85

12.Arrays


An array is a data structure that contains a number of variables which are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.

An array has a rank which determines the number of indices associated with each array element. The rank of an array is also referred to as the dimensions of the array. An array with a rank of one is called a single-dimensional array. An array with a rank greater than one is called a multi-dimensional array. Specific sized multi-dimensional arrays are often referred to as two-dimensional arrays, three-dimensional arrays, and so on.

Each dimension of an array has an associated length which is an integral number greater than or equal to zero. The dimension lengths are not part of the type of the array, but rather are established when an instance of the array type is created at run-time. The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices can range from 0 to N – 1 inclusive. The total number of elements in an array is the product of the lengths of each dimension in the array. If one or more of the dimensions of an array have a length of zero, the array is said to be empty.

The element type of an array can be any type, including an array type.


12.1Array types


An array type is written as a non-array-type followed by one or more rank-specifiers:

array-type:
non-array-type rank-specifiers


non-array-type:
type


rank-specifiers:
rank-specifier
rank-specifiers rank-specifier


rank-specifier:
[ dim-separatorsopt ]


dim-separators:
,
dim-separators ,

A non-array-type is any type that is not itself an array-type.

The rank of an array type is given by the leftmost rank-specifier in the array-type: A rank-specifier indicates that the array is an array with a rank of one plus the number of “,” tokens in the rank-specifier.

The element type of an array type is the type that results from deleting the leftmost rank-specifier:



  • An array type of the form T[R] is an array with rank R and a non-array element type T.

  • An array type of the form T[R][R1]...[RN] is an array with rank R and an element type T[R1]...[RN].

In effect, the rank-specifiers are read from left to right before the final non-array element type. The type int[][,,][,] is a single-dimensional array of three-dimensional arrays of two-dimensional arrays of int.

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


12.1.1The System.Array type


The type System.Array is the abstract base type of all array types. An implicit reference conversion (§6.1.6) exists from any array type to System.Array, and an explicit reference conversion (§6.2.4) 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.1.2Arrays and the generic IList interface


A one-dimensional array T[] implements the interface System.Collections.Generic.IList (IList for short) and its base interfaces. Accordingly, there is an implicit conversion from T[] to IList and its base interfaces. In addition, if there is an implicit reference conversion from S to T then S[] implements IList and there is an implicit reference conversion from S[] to IList and its base interfaces (§6.1.6). If there is an explicit reference conversion from S to T then there is an explicit reference conversion from S[] to IList and its base interfaces (§6.2.4). For example:

using System.Collections.Generic;

class Test
{
static void Main() {
string[] sa = new string[5];
object[] oa1 = new object[5];
object[] oa2 = sa;

IList lst1 = sa; // Ok


IList lst2 = oa1; // Error, cast needed
IList lst3 = sa; // Ok
IList lst4 = oa1; // Ok

IList lst5 = (IList)oa1; // Exception


IList lst6 = (IList)oa2; // Ok
}
}

The assignment lst2 = oa1 generates a compile-time error since the conversion from object[] to IList is an explicit conversion, not implicit. The cast (IList)oa1 will cause an exception to be thrown at runtime since oa1 references an object[] and not a string[]. However the cast (IList)oa2 will not cause an exception to be thrown since oa2 references a string[].

Whenever there is an implicit or explicit reference conversion from S[] to IList, there is also an explicit reference conversion from IList and its base interfaces to S[] (§6.2.4).

When an array type S[] implements IList, some of the members of the implemented interface may throw exceptions. The precise behavior of the implementation of the interface is beyond the scope of this specification.


12.2Array creation


Array instances are created by array-creation-expressions (§7.5.10.4) 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).




Download 3.2 Mb.

Share with your friends:
1   ...   60   61   62   63   64   65   66   67   ...   85




The database is protected by copyright ©ininet.org 2024
send message

    Main page