55

Whilst playing around in an open source project, my attempt to ToString a DateTime object was thwarted by the compiler. When I jumped to the definition, I saw this:

public DateTime? timestamp;

Might someone please enlighten me on what this is called and why it might be useful?

glenneroo
  • 1,908
  • 5
  • 30
  • 49

4 Answers4

74

This is a nullable type. Nullable types allow value types (e.g. ints and structures like DateTime) to contain null.

The ? is syntactic sugar for Nullable<DateTime> since it's used so often.

To call ToString():

if (timstamp.HasValue) {        // i.e. is not null
    return timestamp.Value.ToString();
}
else {
    return "<unknown>";   // Or do whatever else that makes sense in your context
}
Adrian
  • 33
  • 6
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • you could use `??` operator instead of if/else no ? – v.oddou Apr 03 '14 at 08:29
  • @v.oddou: Yes, although in this example the resulting object is a DateTime that we want to format as a string (and it doesn't really make sense to have a default date/time if there isn't one stored in the nullable structure). – Cameron Apr 03 '14 at 13:44
  • 1
    @v.oddou Idunno - that would be awful lot of question mark-y syntactic sugar for one varibale, don't you think? – Code Jockey Sep 25 '14 at 14:45
  • @Cameron what I don't understand is, your proposal is exactly what MSDN says the [overloaded `Nullable<>.ToString()`](http://msdn.microsoft.com/en-us/library/9hd15ket(v=vs.110).aspx) is supposed to do... (?) QUOTE: _"The text representation of the value of the current Nullable object if the HasValue property is true, or an empty string ("") if the HasValue property is false."_ – Code Jockey Sep 25 '14 at 14:52
  • @CodeJockey: Huh, seems you're right. You could simply call `ToString()` directly in this case. The point of my example, though, was not to rewrite `ToString()`, but rather to show how a nullable variable might be used. I'll change the string in the `else` to make it less silly ;-) – Cameron Sep 25 '14 at 15:40
  • That makes more sense as an "overload", but it still leaves me wondering how the OP had an issue to begin with: _"...my attempt to ToString a DateTime object..."_ - might have had a different issue, but it was apparently helpful to the OP to know what the `?` was... :-) – Code Jockey Sep 26 '14 at 21:01
  • Not sure for OP, but I had a similar issue: `DateTime` supported `DateTime.ToString("M/d/yyyy");` but when it was a `DateTime?`, there was no overload `ToString("some string")` - only `ToString()`. That was somewhat of a problem for me. Is that expected? – Don Cheadle Jun 22 '15 at 14:03
  • @mmcrae: Yes, that's expected. `Nullable` provides a uniform API regardless of `T`. – Cameron Jun 22 '15 at 15:00
  • so I guess only way to get the real `DateTime.ToString("M/d/yyyy")` would be to check `HasValue` and then call `Value` to get actual `DateTime`? – Don Cheadle Jun 22 '15 at 15:19
  • @mmcrae: Yes, that's right. It's up to you to decide what string representation you want when the `DateTime?` is null. – Cameron Jun 22 '15 at 16:14
9

? makes a value type (int, bool, DateTime, or any other struct or enum) nullable via the System.Nullable<T> type. DateTime? means that the variable is a System.Nullable<DateTime>. You can assign a DateTime or the value null to that variable. To check if the variable has a value, use the HasValue property and to get the actual value, use the Value property.

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
5

That is a shortcut for Nullable<DateTime>. Value types, like DateTime cannot be null; Nullable<> wraps the value type so that you have an object with a HasValue property and other convenient features.

Jacob
  • 77,566
  • 24
  • 149
  • 228
-2

it is nullable datetime

Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66