Skip to content

Commit 264734a

Browse files
committed
Add support for nullable arrays, arrays of nullables, and nullable arrays of nullable.
1 parent 22a5775 commit 264734a

File tree

10 files changed

+3878
-24
lines changed

10 files changed

+3878
-24
lines changed

standard/arrays.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The element type of an array can itself be an array type ([§17.2.1](arrays.md#1
1212
>
1313
> <!-- Example: {template:"code-in-main-without-using", name:"PascalArrayDeclarations"} -->
1414
> ```csharp
15-
> int[][] pascals =
15+
> int[][] pascals =
1616
> {
1717
> new int[] {1},
1818
> new int[] {1, 1},
@@ -46,9 +46,18 @@ In effect, the *rank_specifier*s are read from left to right *before* the final
4646
4747
> *Example*: The type in `T[][,,][,]` is a single-dimensional array of three-dimensional arrays of two-dimensional arrays of `int`. *end example*
4848
49-
At run-time, a value of an array type can be `null` or a reference to an instance of that array type.
49+
An array type is a reference type and therefore can be a nullable reference type ([§8.9.3](types.md#893-nullable-reference-types)). Likewise the element type of an array can be a nullable reference type or a nullable value type ([§8.3.12](types.md#8312-nullable-value-types)):
5050
51-
> *Note*: Following the rules of17.6](arrays.md#176-array-covariance), the value may also be a reference to a covariant array type. *end note*
51+
- An array type of the form `T[R]` is a non-nullable array with rank `R` and a non-array non-nullable element type `T`.
52+
- An array type of the form `T[R]?` is a nullable array with rank `R` and a non-array non-nullable element type `T`.
53+
- An array type of the form `T?[R]` is a non-nullable array with rank `R` and a non-array nullable element type `T`.
54+
- An array type of the form `T?[R]?` is a nullable array with rank `R` and a non-array nullable element type `T`.
55+
56+
At run-time, a value of an array type can be:
57+
58+
- `null`; or
59+
- a reference to an instance of that array type; or
60+
- a reference to an instance of a covariant array type, if by17.6](arrays.md#176-array-covariance) such exists.
5261
5362
### 17.2.2 The System.Array type
5463
@@ -132,15 +141,15 @@ Because of array covariance, assignments to elements of reference type arrays in
132141
> ```csharp
133142
> class Test
134143
> {
135-
> static void Fill(object[] array, int index, int count, object value)
144+
> static void Fill(object[] array, int index, int count, object value)
136145
> {
137146
> for (int i = index; i < index + count; i++)
138147
> {
139148
> array[i] = value;
140149
> }
141150
> }
142151
>
143-
> static void Main()
152+
> static void Main()
144153
> {
145154
> string[] strings = new string[100];
146155
> Fill(strings, 0, 100, "Undefined");
@@ -169,7 +178,7 @@ array_initializer
169178
variable_initializer_list
170179
: variable_initializer (',' variable_initializer)*
171180
;
172-
181+
173182
variable_initializer
174183
: expression
175184
| array_initializer

standard/types.md

+30-18
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ array_type
5757
: non_array_type rank_specifier+
5858
;
5959
60-
non_array_type
61-
: value_type
62-
| class_type
63-
| interface_type
64-
| delegate_type
65-
| 'dynamic'
66-
| type_parameter
67-
| pointer_type // unsafe code support
68-
;
69-
7060
rank_specifier
7161
: '[' ','* ']'
7262
;
@@ -83,6 +73,28 @@ nullable_type_annotation
8373
: '?'
8474
;
8575
76+
non_array_type
77+
: array_reference_type
78+
| value_type
79+
| type_parameter
80+
| pointer_type // unsafe code support
81+
;
82+
83+
array_reference_type
84+
: non_nullable_array_reference_type
85+
| nullable_array_reference_type
86+
;
87+
88+
nullable_array_reference_type
89+
: non_nullable_array_reference_type nullable_type_annotation
90+
;
91+
92+
non_nullable_array_reference_type
93+
: class_type
94+
| interface_type
95+
| delegate_type
96+
| 'dynamic'
97+
;
8698
```
8799

88100
*pointer_type* is available only in unsafe code ([§23.3](unsafe-code.md#233-pointer-types)). *nullable_reference_type* is discussed further in [§8.9](types.md#89-reference-types-and-nullability).
@@ -201,11 +213,11 @@ floating_point_type
201213
tuple_type
202214
: '(' tuple_type_element (',' tuple_type_element)+ ')'
203215
;
204-
216+
205217
tuple_type_element
206218
: type identifier?
207219
;
208-
220+
209221
enum_type
210222
: type_name
211223
;
@@ -327,7 +339,7 @@ C# supports nine integral types: `sbyte`, `byte`, `short`, `ushort`, `int`, `uin
327339
- The `ulong` type represents unsigned 64-bit integers with values from `0` to `18446744073709551615`, inclusive.
328340
- The `char` type represents unsigned 16-bit integers with values from `0` to `65535`, inclusive. The set of possible values for the `char` type corresponds to the Unicode character set.
329341
> *Note*: Although `char` has the same representation as `ushort`, not all operations permitted on one type are permitted on the other. *end note*
330-
342+
331343
All signed integral types are represented using twos complement format.
332344
333345
The *integral_type* unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit precision, signed 64-bit precision, or unsigned 64-bit precision, as detailed in [§12.4.7](expressions.md#1247-numeric-promotions).
@@ -539,7 +551,7 @@ type_argument_list
539551
540552
type_arguments
541553
: type_argument (',' type_argument)*
542-
;
554+
;
543555
544556
type_argument
545557
: type
@@ -925,10 +937,10 @@ A compiler can update the null state of a variable as part of its analysis.
925937
> int length = p.Length; // Warning: p is maybe null
926938
>
927939
> string s = p; // No warning. p is not null
928-
>
940+
>
929941
> if (s != null)
930942
> {
931-
> int l2 = s.Length; // No warning. s is not null
943+
> int l2 = s.Length; // No warning. s is not null
932944
> }
933945
> int l3 = s.Length; // Warning. s is maybe null
934946
> }
@@ -1031,11 +1043,11 @@ A compiler may use any expression that dereferences a variable, property, or eve
10311043
> public class C
10321044
> {
10331045
> private C? child;
1034-
>
1046+
>
10351047
> public void M()
10361048
> {
10371049
> _ = child.child.child; // Warning. Dereference possible null value
1038-
> var greatGrandChild = child.child.child; // No warning.
1050+
> var greatGrandChild = child.child.child; // No warning.
10391051
> }
10401052
> }
10411053
> ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sample: Arrays of NRT
2+
3+
Samples for nullable arrays, arrays of nullables, and nullable arrays of nullables.

0 commit comments

Comments
 (0)