You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/arrays.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@ An array has a rank that determines the number of indices associated with each a
9
9
The element type of an array can itself be an array type ([§16.2.1](arrays.md#1621-general)). Such arrays of arrays are distinct from multi-dimensional arrays and can be used to represent “jagged arrays”.
10
10
11
11
> *Example*:
12
+
>
12
13
> ```csharp
13
14
>int[][] pascals=
14
15
> {
@@ -18,6 +19,7 @@ The element type of an array can itself be an array type ([§16.2.1](arrays.md#1
@@ -60,6 +62,7 @@ A single-dimensional array `T[]` implements the interface System.Collections.Gen
60
62
Similarly, a single-dimensional array `T[]` also implements the interface `System.Collections.Generic.IReadOnlyList<T>` (`IReadOnlyList<T>` for short) and its base interfaces. Accordingly, there is an implicit conversion from `T[]` to `IReadOnlyList<T>` and its base interfaces. In addition, if there is an implicit reference conversion from `S` to `T` then `S[]` implements `IReadOnlyList<T>` and there is an implicit reference conversion from `S[]` to `IReadOnlyList<T>` and its base interfaces ([§10.2.8](conversions.md#1028-implicit-reference-conversions)). If there is an explicit reference conversion from `S` to `T` then there is an explicit reference conversion from `S[]` to `IReadOnlyList<T>` and its base interfaces ([§10.3.5](conversions.md#1035-explicit-reference-conversions)).
61
63
62
64
> *Example*: Forexample:
65
+
>
63
66
> ```csharp
64
67
> usingSystem.Collections.Generic;
65
68
>
@@ -88,6 +91,7 @@ Similarly, a single-dimensional array `T[]` also implements the interface `Syste
Wheneverthereisanimplicitorexplicitreferenceconversionfrom `S[]` to `IList<T>`, thereisalsoanexplicitreferenceconversionfrom `IList<T>` anditsbaseinterfacesto `S[]` ([§10.3.5](conversions.md#1035-explicit-reference-conversions)).
@@ -121,6 +125,7 @@ For any two *reference_type*s `A` and `B`, if an implicit reference conversion
121
125
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 ([§11.18.2](expressions.md#11182-simple-assignment)).
122
126
123
127
> *Example*:
128
+
>
124
129
> ```csharp
125
130
> classTest
126
131
> {
@@ -141,6 +146,7 @@ Because of array covariance, assignments to elements of reference type arrays in
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.
168
175
169
176
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, or is inferred from the expressions in the array 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,
170
177
171
178
```csharp
172
179
int[] a= {0, 2, 4, 6, 8};
173
180
```
181
+
174
182
it is simply shorthand for an equivalent array creation expression:
175
183
176
184
```csharp
177
185
int[] a=newint[] {0, 2, 4, 6, 8};
178
186
```
187
+
179
188
For a single-dimensional array, the array initializer shall consist of a sequence of expressions, each having an implicit conversion to the element type of the array ([§10.2](conversions.md#102-implicit-conversions)). 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.
180
189
181
190
> *Example*: The array initializer above creates an `int[]` instance of length 5 and then initializes the instance with the following values:
>Here, theinitializerfor `y` results in a compile-time error because the dimension length expression is not a constant, and the initializer for `z` results in a compile-time error because the length and the number of elements in the initializer do not agree. *end example*
0 commit comments