Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a questionmark on the private variable definition?

Tags:

c#

nullable

I am reading an article about the MVVP Pattern and how to implement it with WPF. In the source code there are multiple lines where I cannot figure out what the question marks in it stand for.

private DateTime? _value; 

What does the ? mean in the definition? I tried to find it in the help from VS but failed.

like image 455
Booser Avatar asked Feb 24 '10 13:02

Booser


People also ask

What does a question mark after a variable mean?

Question marks on TypeScript variable are used to mark that variable as an optional variable.

What does a question mark mean after a variable 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 is the purpose of the question mark in this code var rocks Int 3?

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

What does putting a question mark after a parameter name DP?

Question mark means "optional". So it's a shorthand for "parameter: type | undefined = undefined"..


2 Answers

It's a nullable value. Structs, by default, cannot be nullable, they must have a value, so in C# 2.0, the Nullable<T> type was introduced to the .NET Framework.

C# implements the Nullable<T> type with a piece of syntactic sugar, which places a question mark after the type name, thus making the previously non-nullable type, nullable.

like image 70
David Morton Avatar answered Sep 19 '22 16:09

David Morton


That means the type is Nullable.

like image 32
Anvaka Avatar answered Sep 17 '22 16:09

Anvaka