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: docs/csharp/nullable-references.md
+55-13Lines changed: 55 additions & 13 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
2
title: Nullable reference types
3
3
description: This article provides an overview of nullable reference types, added in C# 8. You'll learn how the feature provides safety against null reference exceptions, for new and existing projects.
4
-
ms.date: 12/03/2018
4
+
ms.date: 02/19/2019
5
5
---
6
6
# Nullable reference types
7
7
@@ -19,6 +19,8 @@ This new feature provides significant benefits over the handling of reference va
19
19
-**A reference can be null**. No warnings are issued when a reference type is initialized to null, or later assigned to null.
20
20
-**A reference is assumed to be not null**. The compiler doesn't issue any warnings when reference types are dereferenced. (With nullable references, the compiler issues warnings whenever you dereference a variable that may be null).
21
21
22
+
With the addition of nullable reference types, you can declare your intent more clearly. The `null` value is the correct way to represent that a variable doesn't refer to a value. Don't use this feature to remove all `null` values from your code. Rather, you should declare your intent to the compiler and other developers that read your code. By declaring your intent, the compiler informs you when you write code that is inconsistent with that intent.
23
+
22
24
A **nullable reference type** is noted using the same syntax as [nullable value types](programming-guide/nullable-types/index.md): a `?` is appended to the type of the variable. For example, the following variable declaration represents a nullable string variable, `name`:
23
25
24
26
```csharp
@@ -35,18 +37,57 @@ name!.Length;
35
37
36
38
You can read details about this operator in the [draft nullable reference types](https://github.com/dotnet/csharplang/blob/master/proposals/nullable-reference-types-specification.md#the-null-forgiving-operator) specification proposal on GitHub.
37
39
40
+
## Nullability of types
41
+
42
+
Any reference type can have one of four *nullabilities*, which describes when warnings are generated:
43
+
44
+
-*Nonnullable*: Null can't be assigned to variables of this type. Variables of this type don't need to be null-checked before dereferencing.
45
+
-*Nullable*: Null can be assigned to variables of this type. Dereferencing variables of this type without first checking for `null` causes a warning.
46
+
-*Oblivious*: This is the pre-C# 8 state. Variables of this type can be dereferenced or assigned without warnings.
47
+
-*Unknown*: This is generally for type parameters where constraints don't tell the compiler that the type must be *nullable* or *nonnullable*.
48
+
49
+
The nullability of a type in a variable declaration is controlled by the *nullable context* in which the variable is declared.
50
+
38
51
## Nullable contexts
39
52
40
-
Nullable contexts enable fine-grained control for how the compiler interprets reference type variables. The nullable context of any given source line is `enabled` or `disabled`. You can think of the pre-C# 8 compiler as compiling all your code in a `disabled` nullable context: Any reference type may be null. Warnings are not generated when variables of a reference type are dereferenced.
53
+
Nullable contexts enable fine-grained control for how the compiler interprets reference type variables. The **nullable annotation context** of any given source line is `enabled` or `disabled`. You can think of the pre-C# 8 compiler as compiling all your code in a `disabled` nullable context: Any reference type may be null. The **nullable warnings context** may be set to `enabled`, `disabled`, or `safeonly`. The nullable warnings context specifies the warnings generated by the compiler using its flow analysis.
54
+
55
+
The nullable annotation context and nullable warning context can be set for a project using the `NullableContextOptions` element in your `csproj` file. This element configures how the compiler interprets the nullability of types and what warnings are generated. Valid settings are:
41
56
42
-
The nullable context is controlled using a new pragma:
57
+
-`enable`: The nullable annotation context is **enabled**. The nullable warning context is **enabled**.
58
+
- Variables of a reference type, `string` for example, are non-nullable. All nullability warnings are enabled.
59
+
-`disable`: The nullable annotation context is **disabled**. The nullable warning context is **disabled**.
60
+
- Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
61
+
-`safeonly`: The nullable annotation context is **enabled**. The nullable warning context is **safeonly**.
62
+
- Variables of a reference type are nonnullable. All safety nullability warnings are enabled.
63
+
-`warnings`: The nullable annotation context is **disabled**. The nullable warning context is **enabled**.
64
+
- Variables of a reference type are oblivious. All nullability warnings are enabled.
65
+
-`safeonlywarnings`: The nullable annotation context is **disabled**. The nullable warning context is **safeonly**.
66
+
- Variables of a reference type are oblivious. All safety nullability warnings are enabled.
43
67
44
-
-`#nullable enable` sets the nullable context to enabled.
45
-
-`#nullable disable` sets the nullable context to disabled.
68
+
You can also use directives to set these same contexts anywhere in your project:
69
+
70
+
-`#nullable enable`: Sets the nullable annotation context and nullable warning context to **enabled**.
71
+
-`#nullable disable`: Sets the nullable annotation context and nullable warning context to **disabled**.
72
+
-`#nullable safeonly`: Set the nullable annotation context to **enabled**, and the warning context to **safeonly**.
73
+
-`#nullable restore`: Restores the nullable annotation context and nullable warning context to the project settings.
74
+
-`#pragma warning disable nullable`: Set the nullable warning context to **disabled**.
75
+
-`#pragma warning enable nullable`: Set the nullable warning context to **enabled**.
76
+
-`#pragma warning restore nullable`: Restores the nullable warning context to the project settings.
77
+
-`#pragma warning safeonly nullable`: Sets the nullable warning context to **safeonly**.
78
+
79
+
The default nullable annotation and warning contexts are `disabled`. That decision means that your existing code compiles without changes and without generating any new warnings.
80
+
81
+
The differences between the `enabled` and `safeonly` nullable warning contexts are warnings for assigning a nullable reference to a non-nullable reference. The following assignment generates a warning in an `enabled` warning context, but not a `safeonly` warning context. However, the second line, where `s` is dereferenced, generates a warning in a `safeonly` context:
82
+
83
+
```csharp
84
+
strings=null; // warning when nullable warning context is enabled.
85
+
vartxt=s.ToString(); // warning when nullable warnings context is safeonly, or enabled.
86
+
```
46
87
47
-
The default nullable context is `disabled`. That decision means that your existing code compiles without changes and without generating any new warnings.
88
+
### Nullable annotation context
48
89
49
-
The compiler uses the following rules in a disabled nullable context:
90
+
The compiler uses the following rules in a disabled nullable annotation context:
50
91
51
92
- You can't declare nullable references in a disabled context.
52
93
- All reference variables may be assigned to null.
@@ -55,25 +96,26 @@ The compiler uses the following rules in a disabled nullable context:
55
96
56
97
The behavior is the same as previous versions of C#.
57
98
58
-
The compiler uses the following rules in an enabled nullable context:
99
+
The compiler uses the following rules in an enabled nullable annotation context:
59
100
60
101
- Any variable of a reference type is a **non-nullable reference**.
61
102
- Any non-nullable reference may be dereferenced safely.
62
103
- Any nullable reference type (noted by `?` after the type in the variable declaration) may be null. Static analysis determines if the value is known to be non-null when it is dereferenced. If not, the compiler warns you.
63
104
- You can use the null-forgiving operator to declare that a nullable reference isn't null.
64
105
65
-
A richer grammar is proposed for nullable contexts and will be available in future previews. For more information about nullable contexts, see the [nullable reference specification](https://github.com/dotnet/csharplang/blob/master/proposals/nullable-reference-types-specification.md#nullable-contexts) draft.
106
+
In an enabled nullable annotation context, the `?` character appended to a reference type declares a **nullable reference type**. The **null forgiveness operator** (`!`) may be appended to an expression to declare that the expression isn't null.
66
107
67
-
## Null states of nullable references
108
+
## Nullable warning context
68
109
69
-
The compiler uses static analysis to determine the **null state** of any nullable reference. The null state is either **not null** or **maybe null**. If you dereference a nullable reference when the compiler has determined it's **maybe null**, the compiler warns you. The state of a nullable reference is **maybe null** unless the compiler can determine one of two conditions:
110
+
The nullable warning context is distinct from the nullable annotation context. Warnings can be enabled even when the new annotations are disabled. The compiler uses static flow analysis to determine the **null state** of any reference. The null state is either **not null** or **maybe null** when the *nullable warning context* isn't **disabled**. If you dereference a reference when the compiler has determined it's **maybe null**, the compiler warns you. The state of a reference is **maybe null** unless the compiler can determine one of two conditions:
70
111
71
112
1. The variable has been definitely assigned to a non-null value.
72
-
1. The variable has been checked against null before de-referencing it.
113
+
1. The variable or expression has been checked against null before de-referencing it.
73
114
74
-
The compiler enforces that non-nullable references may never be set to the null value. You must initialize them to a non-null value. If you don't, the compiler warns that a non-nullable reference wasn't initialized. The compiler also warns you whenever you assign a non-nullable reference to a nullable reference that **may be null**. That implies you can only assign a non-nullable reference to a nullable reference if that nullable reference is **not null**.
115
+
The compiler generates warnings whenever you dereference a variable or expression in a **maybe null** state when the nullable warning context is `enabled` or `safeonly`. Furthermore, warnings are generated when a **maybe null** variable or expression is assigned to a nonnullable reference type when the nullable annotation context is `enabled`.
0 commit comments