csharp-nullable-reference-types

Guides adoption and annotation of nullable reference types in C# codebases.

1.1k|101|Updated Nov 12, 2025
One-click install
npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill csharp-nullable-reference-types
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-nullable-reference-types
Source: https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/csharp-nullable-reference-types
Command: npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill csharp-nullable-reference-types

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

C# codebases without nullable reference types (NRT) suffer from runtime NullReferenceException crashes and ambiguous API contracts, and enabling NRT on legacy projects produces overwhelming warning noise without a clear migration strategy.

Core Features & Use Cases

  • NRT Adoption and Migration: Incremental rollout strategies using <Nullable>enable</Nullable>, per-file #nullable directives, and warnings-only or annotations-only phases for legacy codebases.
  • Nullable Attribute Catalog: Complete guidance on System.Diagnostics.CodeAnalysis attributes including AllowNull, DisallowNull, MaybeNull, NotNull, NotNullWhen, NotNullIfNotNull, MemberNotNull, DoesNotReturn, and the C# 14 field keyword null-resilience rules.
  • API Design and Compatibility: Rules for nullability in public signatures, generic constraints, and source-breaking annotation changes in shipped libraries, including polyfill tradeoffs for netstandard2.0 and .NET Framework targets.
  • Use Case: When modernizing a legacy .NET library, use this Skill to enable NRT project-by-project, annotate Try-pattern methods with [NotNullWhen(true)], and wrap unannotated ORM APIs so downstream callers get full flow analysis.

Quick Start

Ask the assistant to enable nullable reference types in your C# project and fix the resulting CS86xx warnings using proper annotations and attributes.

Frequently Asked Questions about csharp-nullable-reference-types

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enable nullable reference types in an existing C# project?

Add <Nullable>enable</Nullable> to the project file for new code, or migrate incrementally by enabling per-project as you touch it, using <Nullable>annotations</Nullable> or <Nullable>warnings</Nullable> to split the rollout, or placing #nullable enable at the top of individual files.

How do I annotate a Try-pattern method so callers get null flow analysis?

Annotate the out parameter with [NotNullWhen(true)], for example bool TryGetMessage(string key, [NotNullWhen(true)] out string? message). When the method returns true, the compiler treats the out value as non-null at the call site.

When should I use the null-forgiving operator in C#?

Use ! only when a real invariant guarantees non-null and the compiler cannot see it, such as an ORM that always returns initialized entities. Prefer narrowing with guard clauses, pattern matching, or nullable attributes instead of suppressing warnings with !.

Do nullable attributes work on netstandard2.0 or .NET Framework?

The attributes are missing from netstandard2.0 and .NET Framework, and partially missing from netstandard2.1. Options include #if-gating annotations, a conditional PolySharp reference with PrivateAssets="all", or raising the TFM floor, but confirm the tradeoffs before adding any dependency.

Why does my non-nullable array or struct still contain null at runtime?

Arrays of non-nullable references are created with all elements null and the compiler does not warn on reads, and default(struct) skips initialization of non-nullable reference fields. Fill arrays before use and provide factories or constructors for such structs.

What does the C# 14 field keyword do for nullable properties?

The field keyword lets a getter like string Prop => field ??= Compute() reference a compiler-synthesized backing field. The compiler's null-resilience analysis avoids CS8618 constructor warnings, and [field: AllowNull, MaybeNull] handles non-resilient getters.