27

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.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Booser
  • 576
  • 2
  • 9
  • 25

6 Answers6

50

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.

David Morton
  • 16,338
  • 3
  • 63
  • 73
10

That means the type is Nullable.

Anvaka
  • 15,658
  • 2
  • 47
  • 56
9

cannot be null

DateTime                        
DateTime dt = null;   // Error: Cannot convert null to 'System.DateTime'
                         because it is a  non-nullable value type 

can be null

DateTime? / Nullable<DateTime>  
DateTime? dt = null;  // no problems
Asad
  • 21,468
  • 17
  • 69
  • 94
6

This is a nullable type, you can assign null to it

Svetlozar Angelov
  • 21,214
  • 6
  • 62
  • 67
3

It means that the field is a Nullable<DateTime>, i.e. a DateTime that can be null

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
3

Private DateTime? _value - means that the _value is nullable. check out this link for a better explanation.

http://davidhayden.com/blog/dave/archive/2005/05/23/1047.aspx

Hope this helps.

Thanks, Raja

Raja
  • 3,608
  • 3
  • 28
  • 39