Sprotty
Sprotty

Reputation: 5973

What does this mean 'string? message'

I just came across this looking at the definition for the PlatformNotSupportedException class.

What does string? message mean. As far as I was are the ? is short hand for Nullable<>, but Nullable<> can only be applied to struct's and string and Exception are classes.

My best guess is its an optional parameter declared like string message = null, if thats the case why not just show it and the default value?

public PlatformNotSupportedException(string? message, Exception? inner);

Upvotes: 0

Views: 994

Answers (1)

StepUp
StepUp

Reputation: 38094

It is a way to declare Nullable Reference Types. The syntax to expect NULL in C# 8:

To begin, there needs to be a syntax for distinguishing when a reference type should expect null and when it shouldn’t. The obvious syntax for allowing null is using the ? as a nullable declaration—both for a value type and a reference type. By including support on reference types, the developer is given a way to opt-in for null with, for example:

string? text = null;

In addition, we can declare non-nullable reference type:

string! text = "It is non nullable"

Upvotes: 3

Related Questions