Agent skill · aaronontheweb

csharp-nullable-reference-types

Guidelines for introducing and using nullable reference types (NRT) and System.Diagnostics.CodeAnalysis nullable attributes in C# / .NET codebases. Covers the nullability model, flow analysis, the null-forgiving operator, API design rules, the full attribute catalog (AllowNull, DisallowNull, MaybeNull, NotNull, NotNullWhen, MaybeNullWhen, NotNullIfNotNull, MemberNotNull, MemberNotNullWhen, DoesNotReturn, DoesNotReturnIf), the C# 14 field keyword, incremental migration of legacy codebases, and a code-generation checklist.

What it needs

About 6k tokens when loaded.

What this skill does

C# Nullable Reference Types When to Use Introducing nullable reference types (NRT) into a codebase that has not yet adopted them Writing or refactoring C# code that uses T? / nullable annotations Annotating APIs with System.Diagnostics.CodeAnalysis nullable attributes Designing public/internal APIs where nullability contracts matter Wrapping unannotated or legacy APIs so downstream callers still benefit from NRT Reviewing code for correct null-state analysis, guard helpers, and the field keyword Core Goals Prevent NullReferenceException at runtime by making null intent explicit in signatures. Express contracts the type system cannot represent directly using the official nullable attributes. Adopt NRT incrementally in legacy codebases without a big-bang rewrite. Core Nullability Model Non-nullable vs nullable string — non-nullable reference. The compiler assumes instances are never null; assigning null or a maybe-null value produces a warning. string? — nullable reference. The variable may be null; the compiler requires a null check before dereference. Null-state analysis (flow) The compiler tracks whether a reference is definitely non-null or maybe null. Null checks and assignments update this state. Introduce explicit null checks (if (x != null), is not null, pattern matching) before dereferencing nullable values. Narrow nullability early and keep the non-null state alive. Null-conditional assignment (C# 14) lets you write customer?.Order = CreateOrder(); — the right side is evaluated only when the receiver is non-null. Null-forgiving operator (!) x! tells the compiler "treat x as non-null here." It affects analysis only, not runtime behavior. Use ! only when a real invariant guarantees non-null and the compiler cannot see it. Do not use ! as a general fix for warnings. Prefer refactoring control flow, adding attributes, or proper member initialization. …

How to use it

Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:

@skills aaronontheweb/csharp-nullable-reference-types

View the source on GitHub

Browse the @skills marketplace