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

Merged
merged 2 commits into from
Jul 8, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 3, 2025

The tutorial incorrectly stated that non-nullable reference type variables "can't be null" at runtime, which is misleading. While nullable reference types provide valuable compile-time warnings, they cannot prevent null values at runtime, especially for public APIs.

Changes Made

Primary fix (line 127):

  • Before: "Its value can't be null."
  • After: "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."

Secondary improvement (line 206):

  • Added clarification that "The compiler's static analysis helps ensure these design contracts are followed" to be more precise about the compiler's role.

Why This Matters

The original statement was problematic because:

  1. Compiler warnings ≠ runtime protection
  2. Public APIs can receive null values from callers who haven't opted into nullable reference types
  3. Malicious or careless code can still pass null values
  4. The overstatement could lead developers to skip necessary argument validation

The corrected documentation now accurately explains the relationship between compile-time analysis and runtime behavior, while providing practical guidance for public API design.

Fixes #22704.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


Internal previews

📄 File 🔗 Preview link
docs/csharp/tutorials/nullable-reference-types.md docs/csharp/tutorials/nullable-reference-types

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Jul 3, 2025
@Copilot Copilot AI changed the title [WIP] Article contains wrongful information Fix overstatement about nullable reference types runtime protection Jul 3, 2025
@Copilot Copilot AI requested a review from BillWagner July 3, 2025 16:09
Copilot finished work on behalf of BillWagner July 3, 2025 16:09
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This LGTM.

@BillWagner BillWagner marked this pull request as ready for review July 7, 2025 17:46
@BillWagner BillWagner requested a review from a team as a code owner July 7, 2025 17:46
@BillWagner BillWagner requested a review from IEvangelist July 7, 2025 17:47
@BillWagner BillWagner enabled auto-merge (squash) July 8, 2025 14:02
@BillWagner BillWagner merged commit af845c8 into main Jul 8, 2025
21 checks passed
@BillWagner BillWagner deleted the copilot/fix-22704 branch July 8, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Article contains wrongful information
3 participants