Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 534
Kuba K Avatar asked Oct 04 '21 09:10

Kuba K


People also ask

What does a question mark after a type mean?

It is a shorthand for Nullable<int> . Nullable<T> is used to allow a value type to be set to null . Value types usually cannot be null.

What does a question mark after a variable mean in C#?

With C# 8, the placing of a question mark character on ANY type will now be treated as an indicator of weather a variable may or may not be null, not in terms of preventing compilation outright, but in terms of how the compiler responds and warns about it.

What does a question mark mean in C sharp?

The conditional operator ?: , also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false , as the following example shows: C# Copy.

What is bool with a question mark?

Answers. It's C# shorthand for Nullable<bool>, which allows a null value to be assigned to a type that is otherwise a value type, and cannot have a null assigned.


1 Answers

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

like image 83
Ermiya Eskandary Avatar answered Oct 16 '22 14:10

Ermiya Eskandary