Skip to content

Fix overstatement about nullable reference types runtime protection #47090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions docs/csharp/tutorials/nullable-reference-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace NullableIntroduction
}
```

As before, you must initialize the list object to a non-null value or the compiler issues a warning. There are no null checks in the second overload of `AddQuestion` because they aren't needed: You've declared that variable to be non-nullable. Its value can't be `null`.
As before, you must initialize the list object to a non-null value or the compiler issues a warning. There are no null checks in the second overload of `AddQuestion` because the compiler helps enforce the non-nullable contract: You've declared that variable to be non-nullable. While the compiler warns about potential null assignments, runtime null values are still possible. For public APIs, consider adding argument validation even for non-nullable reference types, since client code might not have nullable reference types enabled or could intentionally pass null.

Switch to *Program.cs* in your editor and replace the contents of `Main` with the following lines of code:

Expand Down Expand Up @@ -203,7 +203,7 @@ Finally, add the following loop at the bottom of the `Main` method:

:::code language="csharp" source="./snippets/NullableIntroduction/Program.cs" id="WriteAnswers":::

You don't need any `null` checks in this code because you've designed the underlying interfaces so that they all return non-nullable reference types.
You don't need any `null` checks in this code because you've designed the underlying interfaces so that they all return non-nullable reference types. The compiler's static analysis helps ensure these design contracts are followed.

## Get the code

Expand Down
Loading