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
Fixes#21941: Add description of the default values for arrays of reference types.
Fixes#30348: Edit pass
Fixes#33667: Update the description of the nullable context to match the language used in the C# 8 draft standard
Fixes#36767: Add language on declaring a non-null array of nullable reference types and a nullable array of non-nullable reference types.
Also, perform a general edit pass.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/builtin-types/arrays.md
+21-3Lines changed: 21 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
-
title: "Arrays"
2
+
title: "The array reference type"
3
3
description: Store multiple variables of the same type in an array data structure in C#. Declare an array by specifying a type or specify Object to store any type.
4
-
ms.date: 08/24/2023
4
+
ms.date: 12/09/2024
5
5
helpviewer_keywords:
6
6
- "arrays [C#]"
7
7
- "C# language, arrays"
@@ -26,6 +26,24 @@ You can store multiple variables of the same type in an array data structure. Yo
26
26
type[] arrayName;
27
27
```
28
28
29
+
An array is a reference type, so the array can be a nullable reference type. The element types might be reference types, so an array can be declared to hold nullable reference types. The following example declarations show the different syntax used to declare the nullability of the array or the elements:
30
+
31
+
```csharp
32
+
type?[] arrayName; // non nullable array of nullable element types.
33
+
type[]?arrayName; // nullable array of non-nullable element types.
34
+
type?[]?arrayName; // nullable array of nullable element types.
35
+
```
36
+
37
+
Uninitialized elements in an array are set to the default value for that type:
38
+
39
+
```csharp
40
+
int[] numbers=newint[10]; // All values are 0
41
+
string[] messages=newstring[10]; // All values are null.
42
+
```
43
+
44
+
> [!IMPORTANT]
45
+
> In the preceding example, even though the type is `string[]`, an array of non-nullable strings, the default value for each element is null. The best way to initialize an array to non-null values is to use a [collection expressions](../operators/collection-expressions.md).
46
+
29
47
An array has the following properties:
30
48
31
49
- An array can be [single-dimensional](#single-dimensional-arrays), [multidimensional](#multidimensional-arrays), or [jagged](#jagged-arrays).
@@ -67,7 +85,7 @@ You can pass an initialized single-dimensional array to a method. In the followi
67
85
68
86
## Multidimensional arrays
69
87
70
-
Arrays can have more than one dimension. For example, the following declarations create four arrays: two have two dimensions, two have three dimensions. The first two declarations declare the length of each dimension, but don't initialize the values of the array. The second two declarations use an initializer to set the values of each element in the multidimensional array.
88
+
Arrays can have more than one dimension. For example, the following declarations create four arrays. Two arrays have have two dimensions. Two arrays have three dimensions. The first two declarations declare the length of each dimension, but don't initialize the values of the array. The second two declarations use an initializer to set the values of each element in the multidimensional array.
Copy file name to clipboardExpand all lines: docs/csharp/nullable-references.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ ms.date: 12/09/2024
6
6
---
7
7
# Nullable reference types
8
8
9
-
*Nullable reference types*refers to a group of features that minimize the likelihood that your code causes the runtime to throw <xref:System.NullReferenceException?displayProperty=nameWithType>. *Nullable reference types* includes three features that help you avoid these exceptions, including the ability to explicitly mark a reference type as *nullable*:
9
+
*Nullable reference types*are a group of features that minimize the likelihood that your code causes the runtime to throw <xref:System.NullReferenceException?displayProperty=nameWithType>. Three features that help you avoid these exceptions, including the ability to explicitly mark a reference type as *nullable*:
10
10
11
11
- Improved static flow analysis that determines if a variable might be `null` before dereferencing it.
12
12
- Attributes that annotate APIs so that the flow analysis determines *null-state*.
@@ -57,22 +57,22 @@ You'll learn about:
57
57
58
58
- The compiler's [null-state analysis](#null-state-analysis): how the compiler determines if an expression is not-null, or maybe-null.
59
59
-[Attributes](#attributes-on-api-signatures) that are applied to APIs that provide more context for the compiler's null-state analysis.
60
-
-[Nullable variable annotations](#nullable-variable-annotations) that provide information about your intent for variables. Annotations are useful for fields, parameters and return values to set the default null-state.
60
+
-[Nullable variable annotations](#nullable-variable-annotations) that provide information about your intent for variables. Annotations are useful for fields, parameters, and return values to set the default null-state.
61
61
- The rules governing [generic type arguments](#generics). New constraints were added because type parameters can be reference types or value types. The `?` suffix is implemented differently for nullable value types and nullable reference types.
62
62
- The [Nullable context](#nullable-context) help you migrate large projects. You can enable warnings and annotations in the nullable context in parts of your app as you migrate. After you address more warnings, you can enable both settings for the entire project.
63
63
64
64
Finally, you learn known pitfalls for null-state analysis in `struct` types and arrays.
65
65
66
66
You can also explore these concepts in our Learn module on [Nullable safety in C#](/training/modules/csharp-null-safety).
67
67
68
-
## null-state analysis
68
+
## Null-state analysis
69
69
70
70
***Null-state analysis*** tracks the *null-state* of references. An expression is either *not-null* or *maybe-null*. The compiler determines that a variable is *not-null* in two ways:
71
71
72
-
1. The variable has been assigned a value that is known to be *not-null*.
73
-
1. The variable has been checked against `null` and hasn't been modified since that check.
72
+
1. The variable was assigned a value that is known to be *not-null*.
73
+
1. The variable was checked against `null` and wasn't assigned since that check.
74
74
75
-
Any variable that the compiler hasn't determined as *not-null* is considered *maybe-null*. The analysis provides warnings in situations where you might accidentally dereference a `null` value. The compiler produces warnings based on the *null-state*.
75
+
Any variable that the compiler can't determined as *not-null* is considered *maybe-null*. The analysis provides warnings in situations where you might accidentally dereference a `null` value. The compiler produces warnings based on the *null-state*.
76
76
77
77
- When a variable is *not-null*, that variable can be dereferenced safely.
78
78
- When a variable is *maybe-null*, that variable must be checked to ensure that it isn't `null` before dereferencing it.
@@ -203,7 +203,7 @@ These constraints help provide more information to the compiler on how `T` is us
203
203
204
204
## Nullable context
205
205
206
-
The *nullable context* determines how nullable reference type annotations are handled and what warnings are produced by static null state analysis. The nullable context contains two flags: the *annotation* setting and the *warning* setting.
206
+
The *nullable context* determines how nullable reference type annotations are handled and what warnings are produced by static null state analysis. The nullable context contains two flags: the *annotation* setting and the *warning* setting.
207
207
208
208
Both the *annotation* and *warning* settings are disabled by default for existing projects. Starting in .NET 6, both flags are enabled by default for *new* projects. The reason for two distinct flags for the nullable context is to make it easier to migrate large projects that predate the introduction of nullable reference types.
0 commit comments