CATEGORII DOCUMENTE  


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 singledimensional array, and an array with a rank greater than one is called a multidimensional array. Multidimensional arrays of specific sizes are often referred to by size, as twodimensional arrays, threedimensional 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 runtime. 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.
An array type is written as a nonarraytype followed by one or more rankspecifiers:
arraytype:
nonarraytype rankspecifiers
nonarraytype:
type
rankspecifiers:
rankspecifier
rankspecifiers rankspecifier
rankspecifier:
[ dimseparators_{opt} ]
dimseparators:
,
dimseparators ,
A nonarraytype is any type that is not itself an arraytype.
The rank of an array type is given by the leftmost rankspecifier in the arraytype: A rankspecifier indicates that the array is an array with a rank of one plus the number of “,” tokens in the rankspecifier.
The element type of an array type is the type that results from deleting the leftmost rankspecifier:
An array type of the form T[R] is an array with rank R and a nonarray element type T.
An array type of the form T[R][R_{1}][R_{N}] is an array with rank R and an element type T[R_{1}][R_{N}].
In effect, the rankspecifiers are read from left to right before the final nonarray element type. For example, the type int[][,,][,] is a singledimensional array of threedimensional arrays of twodimensional arrays of int.
At runtime, a value of an array type can be null or a reference to an instance of that array type.
The System.Array type 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 itself is not an arraytype. Rather, it is a classtype from which all arraytypes are derived.
At runtime, a value of type System.Array can be null or a reference to an instance of any array type.
Array instances are created by arraycreationexpressions (§7.5.10.2) or by field or local variable declarations that include an arrayinitializer (§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 arraycreationexpressions are always initialized to their default value (§5.2).
Array elements are accessed using elementaccess expressions (§7.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, 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).
Every array type inherits the members declared by the System.Array type.
For any two referencetypes 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 rankspecifier (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 runtime 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
}
The assignment to array[i] in the
Fill method implicitly includes a runtime 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
Array covariance specifically does not extend to arrays of valuetypes. For example, no conversion exists that permits an int[] to be treated as an object[].
Array initializers may be specified in field declarations (§10.4), local variable declarations (§8.5.1), and array creation expressions (§7.5.10.2):
arrayinitializer:
variableinitializerlist:
variableinitializer
variableinitializerlist , variableinitializer
variableinitializer:
expression
arrayinitializer
An array initializer consists of a sequence of variable initializers, enclosed by “” tokens and separated by “,” tokens. Each variable initializer is an expression or, in the case of a multidimensional 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[] arr = ;
it is simply shorthand for an equivalent array creation expression:
int[] arr = new int[]
For a singledimensional 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 multidimensional 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 of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level. The example:
int[,] b = , , , , };
creates a twodimensional array with a length of five for the leftmost dimension and a length of two for the rightmost dimension:
int[,] b = new int[5, 2];
and then initializes the array instance with the following values:
b[0, 0]
= 0; b[0, 1] = 1;
b[1, 0] = 2; b[1, 1] = 3;
b[2, 0] = 4; b[2, 1] = 5;
b[3, 0] = 6; b[3, 1] = 7;
b[4, 0] = 8; b[4, 1] = 9;
When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length. Some examples:
int i =
3;
int[] x = new int[3] ; // OK
int[] y = new int[i] ; //
Error, i not a constant
int[] z = new int[3] ; //
Error, length/initializer mismatch
Here, the initializer for y is in error because the dimension length expression is not a constant, and the initializer for z is in error because the length and the number of elements in the initializer do not agree.
Politica de confidentialitate 
Vizualizari: 920
Importanta:
Termeni si conditii de utilizare  Contact
© SCRIGROUP 2022 . All rights reserved
Distribuie URL
Adauga cod HTML in site