Nullable Reference Types
Understanding Nullable Reference Types
Non-Nullable by Default
// This string cannot be null
string name = "John";
// Compiler warning: Cannot convert null to non-nullable reference type
string name = null; // ⚠️ CS8600Nullable Types
// This string can be null
string? optionalName = null; // ✅ OK
// Must check for null before using
if (optionalName != null)
{
Console.WriteLine(optionalName.Length);
}Impact on Brighter Code
Commands and Events
Handlers
Message Mappers
V10 Changes
What Changed
Benefits
Migration Guide
Step 1: Enable Nullable Reference Types
Step 2: Address Compiler Warnings
CS8600: Converting null literal or possible null value to non-nullable type
CS8601: Possible null reference assignment
CS8602: Dereference of a possibly null reference
CS8603: Possible null reference return
CS8618: Non-nullable field must contain a non-null value when exiting constructor
Step 3: Update Handler Code
Step 4: Update Message Mappers
Best Practices
1. Use Non-Nullable for Required Properties
2. Validate at Boundaries
3. Use Null-Coalescing for Defaults
4. Document Nullability in XML Comments
5. Avoid Null-Forgiving Operator Unless Certain
6. Use Pattern Matching for Null Checks
7. Consider Required Members (C# 11+)
Common Patterns in Brighter
Command with Validation
Event with Optional Properties
Handler with Optional Dependencies
Troubleshooting
Warning: Treat as Errors
Suppressing Warnings (Not Recommended)
Gradual Migration
Additional Resources
Last updated
Was this helpful?
