0

I have the following enigmatic declaration in C#:

Dictionary<string, DateTime?> badCameras = new Dictionary<string, DateTime?>();

This was written by a programmer no longer here & IS compiling! What ever does the '?' mean following DateTime object? Has it something to do with a struct? I've searched online & am finding nothing? BTW, this is .NET3.5.

Thanks

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

2 Answers2

1

DateTime? is compiler sugar for Nullable<DateTime>. Effectively, nullable types allow for the use of null when dealing with value types that don't normally support null. See here for details.

spender
  • 117,338
  • 33
  • 229
  • 351
1

The ? in that context is a shortcut for Nullable<T>. In your example, its equivalant to:

Dictionary<string, Nullable<DateTime>>

The main purpose is to allow value types to hold the null value.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117