C# 8.0 Features: Nullable Reference Types Explained

On this page

Nullable reference types are the biggest C# 8.0 feature. They help eliminate null reference exceptions at compile time. Here’s how they work.

Enabling Nullable

XML3 lines
<PropertyGroup>
  <Nullable>enable</Nullable>
</PropertyGroup>

The New Syntax

C#11 lines
// Non-nullable (default with feature enabled)
string name = "John";  // Cannot be null

// Nullable 
string? nickname = null;  // Explicitly nullable

// Must check before using
if (nickname != null)
{
    Console.WriteLine(nickname.Length);  // Safe
}

Null Forgiving Operator

C#3 lines
// When you know better than the compiler
string? maybeNull = GetValue();
string definitelyNotNull = maybeNull!;  // Trust me

Benefits

  • Compiler warnings for potential null dereferences
  • Documentation of intent in method signatures
  • Catches bugs before runtime

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Written by

Nithin Mohan TK

Hello Namaste! 👋🏻 I'm an accomplished Enterprise Solution Architect & Engineering Leader with 20+ years of progressive experience architecting, building, and scaling transformative enterprise solutions. ⚜️ Solution Architect / AI & Cloud 📌 Galway, Ireland

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.