Skip to content

Commit 0b79b48

Browse files
committed
freshness pass and bug fixes
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.
1 parent 4906901 commit 0b79b48

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

docs/csharp/language-reference/builtin-types/arrays.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: "Arrays"
2+
title: "The array reference type"
33
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
55
helpviewer_keywords:
66
- "arrays [C#]"
77
- "C# language, arrays"
@@ -26,6 +26,24 @@ You can store multiple variables of the same type in an array data structure. Yo
2626
type[] arrayName;
2727
```
2828

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 = new int[10]; // All values are 0
41+
string[] messages = new string[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+
2947
An array has the following properties:
3048

3149
- 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
6785
6886
## Multidimensional arrays
6987
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.
7189
7290
:::code language="csharp" source="./snippets/shared/Arrays.cs" id="MultiDimensionalArrayDeclaration":::
7391

docs/csharp/nullable-references.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.date: 12/09/2024
66
---
77
# Nullable reference types
88

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*:
1010

1111
- Improved static flow analysis that determines if a variable might be `null` before dereferencing it.
1212
- Attributes that annotate APIs so that the flow analysis determines *null-state*.
@@ -57,22 +57,22 @@ You'll learn about:
5757

5858
- The compiler's [null-state analysis](#null-state-analysis): how the compiler determines if an expression is not-null, or maybe-null.
5959
- [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.
6161
- 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.
6262
- 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.
6363

6464
Finally, you learn known pitfalls for null-state analysis in `struct` types and arrays.
6565

6666
You can also explore these concepts in our Learn module on [Nullable safety in C#](/training/modules/csharp-null-safety).
6767

68-
## null-state analysis
68+
## Null-state analysis
6969

7070
***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:
7171

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.
7474

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*.
7676

7777
- When a variable is *not-null*, that variable can be dereferenced safely.
7878
- 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
203203

204204
## Nullable context
205205

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.
207207

208208
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.
209209

0 commit comments

Comments
 (0)