Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions standard/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,13 @@ Every expression has one of three ***null state***s:

The ***default null state*** of an expression is determined by its type, and the state of the annotations flag when it is declared:

- The default null state of a nullable reference type is:
- Maybe null when its declaration is in text where the annotations flag is enabled.
- Not null when its declaration is in text where the annotations flag is disabled.
- The default null state of a non-nullable reference type is not null.
- The default state of the `null` literal (§6.4.5.7) is maybe null.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RexJaeschke Please file an issue (assign it to me) for expressions without types, referring to this PR.

- The default state of a default value expression (§12.8.21) is not null when the type is a non-nullable value type, otherwise maybe null.
- For all other expressions:
- The default null state of an expression whose type is a nullable reference type is:
- Maybe null when its declaration is in text where the annotations flag is enabled.
- Not null when its declaration is in text where the annotations flag is disabled.
- The default null state of an expression whose type is a non-nullable reference type is not null.

> *Note:* The *maybe default* state is used with unconstrained type parameters when the type is a non-nullable type, such as `string` and the expression `default(T)` is the null value. Because null is not in the domain for the non-nullable type, the state is maybe default. *end note*

Expand Down Expand Up @@ -1168,4 +1171,24 @@ A compiler may issue a warning when nullability differs in either direction in t
>
> *end example*

A compiler may issue a warning when the result of a lifted conversion operator (§10.6.2) is a reference type and is converted to a non-nullable reference type, since the result of a lifted conversion operator is maybe-null.

> <!-- Example: {template:"code-in-class-lib", name:"LiftedOperator", ignoredWarnings:["CS8600"]} -->
> ```csharp
> #nullable enable
> class C
> {
> void M()
> {
> C? a = (int?)null; // No warning
> C b = (int?)null; // Warning: Possible assignment to null
> C c = 1; // No warning
> }
>
> public static implicit operator C(int v) => new C();
> }
> ```
>
> *end example*

**End of conditionally normative text**