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.
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.
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.
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With