csharp-nullable-types

Apply C# nullable types to prevent null reference exceptions.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill csharp-nullable-types
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: csharp-nullable-types
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-csharp/csharp-nullable-types
Command: npx skills add https://github.com/TheBushidoCollective/han --skill csharp-nullable-types

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Explore nullable value and reference types in C#, with safe access and common patterns.

Core Features & Use Cases

  • Nullable Value Types: int? and struct wrappers.
  • Nullable Reference Types: enable and use with caution.
  • Operators & Patterns: null-coalescing and null-conditional operators.

Quick Start

Demonstrate a small example using nullable types and the null-coalescing operator.

Frequently Asked Questions about csharp-nullable-types

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

FAQPage Schema
How do I prevent null reference exceptions in C#?▼

Null reference exceptions in C# can be prevented by enabling nullable reference types (C# 8.0+) and using nullable annotations to declare which variables can be null. Apply null checks, null-coalescing operators (??), and null-conditional access (?.) to safely handle potential null values before dereferencing.

What's the difference between nullable value types and nullable reference types?▼

Nullable value types (int?, struct?) wrap value types to allow null assignment with HasValue and Value properties. Nullable reference types enable compile-time warnings on reference types that might be null. Both use nullable annotations but operate differently: value types remain stack-allocated, reference types remain heap-allocated.

How do I use the null-coalescing operator in C#?▼

The null-coalescing operator (??) returns the left operand if it's not null, otherwise returns the right operand. Use it to provide default values: `string name = user?.Name ?? "Unknown";`. Chain multiple operators for fallback sequences and combine with null-conditional access (?.) for safe property navigation.

Can I use nullable types across methods and properties?▼

Yes. Apply nullable annotations to method parameters, return types, and properties to enforce null-safety checks at compile time. Mark nullable parameters and return types with ?, non-nullable without it. This propagates null expectations across your codebase, helping prevent null reference exceptions at call sites.

What C# versions support nullable reference types?▼

Nullable reference types are available in C# 8.0 and later. Enable them per-project or per-file using `#nullable enable`. Nullable value types (int?, struct?) have been available since C# 2.0, but the modern nullable reference type system requires C# 8.0+ for compile-time null-safety annotations.

How do I safely retrieve values from nullable types in collections?▼

Use null-conditional access with indexers and iterators: `list?[index]?.Property`. For nullable value types, check HasValue before accessing Value. Use LINQ methods like FirstOrDefault with null coalescing for safe retrieval from collections containing nullable elements.