What does a question mark after a reference type mean in C#?

In C# 8.0, nullable reference types were introduced – this allows you to mark reference types as nullable by appending ? to the type name.

In null-state analysis, the code is statically checked to keep track of the null-state of references. This analysis can result in one of the 2 states below:

  • not-null – variable has a non-null value

  • maybe-null – variable can be null or not-null; the analyser can’t determine for sure

This is useful as it helps the compiler provide warnings to help you write code less prone to NullReferenceExceptions and more consumable by other APIs.

For example, the compiler will emit a warning for the below.

#nullable enable
...

string message = null;

// Warning - CS8602 Dereference of a possibly null reference

Console.WriteLine(message.Length);

This is because since message was set to null, its state was tracked as maybe-null. This throws a warning to alert you just in case you’re trying to access the properties of a null object. And yes, you are.

Here’s a different example:

#nullable enable
...

string message = "The quick brown fox jumps over the lazy dog";

// No warning
Console.WriteLine(message.Length);

Since message is known to not be null, its state is set to not-null. The compiler won’t produce a warning as it knows that you can dereference the variable safely. It is definitely not null.

Alternatively, you can also just decide to tell the world that some variables can be null 🙂

Leave a Comment