Kuba K
Kuba K

Reputation: 467

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

What is the purpose of the question mark after the type of the parameter which is a reference type?

e.g one of the constructors for an OverflowException contains string? and Exception? parameters:

public OverflowException(string? message, Exception? innerException);

I don't see a point in marking them as nullable as those are not value types, so they are nullable by default.

What is the point? Does it mean something different?

Note that there is What is the purpose of a question mark after a value type (for example: int? myVariable)? describing behavior for value types - essentially to allow null values for value types, but that is not needed for reference types as they can contain null values already.

Upvotes: 17

Views: 5428

Answers (1)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23692

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 :)

Upvotes: 17

Related Questions