liveBook · Manning

Nullable reference types

As explained in .NET in Action, Second Edition

A nullable reference type is a C# type annotation that allows a reference to represent null and participates in compile-time nullability analysis.

A nullable reference type makes a possible null value explicit to the C# compiler. With `<Nullable>enable</Nullable>` in the project file, write `string?` when a property may be null. The compiler can then warn about potential null problems. The annotation does not change the fact that a reference can hold null at runtime. section 2.8 section 5.2.2

Model an optional command-line value

An `Options` record declares `public string? Text` even though the command-line parser requires the argument. The parser will not create the options object without that argument, but the compiler cannot infer the parser’s guarantee. The nullable annotation resolves the warning by acknowledging what the compiler can establish. section 2.8

Choose an annotation that matches the property’s intended use:

  • Use `string? Title` if a missing title is acceptable. section 5.2.2
  • If a title is required, keep `string Title` and initialize it, or require it through a constructor. A nullable annotation would silence the warning while also allowing an unwanted null title. section 5.2.2
Listing 5.7 Book constructor
public class Book
{
  public int Id { get; set; }          #1
  public string Title { get; set; }    #2
  
  public Book(string title)            #3
  {
    Title = title;                     #4
  }
}

The `Book` constructor shows how to assign a non-nullable `Title` when creating an entity; passing `null` produces a compiler warning. section 5.2.2

Return types need the same care. `Comic? GetComic` reflects both a failed HTTP lookup and the possibility that valid JSON contains the value `null`, which deserializes to null. Callers can check the result before using it. Keep nullable checking enabled rather than disabling it merely to remove warnings. section 4.3 section 5.2.2

How .NET in Action, Second Edition covers it

How deep it goes
Explains annotations, project configuration, warnings, and modeling choices through working examples; stops short of compiler-analysis internals.
Examples
Uses an options property, a book-title constructor, and a nullable comic lookup to expose different reasons for nullability.
What it assumes
Assumes basic familiarity with C# properties while explaining `string?` and the nullable project setting.
What you can do after
Apply it via C# annotations and project settings to address warnings without permitting null where a value is required.
How it connects
Connects nullable annotations to C# properties, constructors, command-line parsing, Entity Framework Core entities, and JSON deserialization.

Also explained in

Compare how 2 books explain nullable reference types →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.