Calling AddDays on a Nullable DateTime object
c#, datetime
Solution
Because `DateTime?` can be in a state representing `null`, you need to consider what to do if it's null.
The most obvious thing to do is to stay with a value of `null` (the day after `null` is `null`).
DateTime? later = ToDate.HasValue
? To_Date.Value.AddDays(numberOfDays)
: (DateTime?)null;
It maybe that there's some meaningful default date you can use, in which case:
DateTime later = (ToDate ?? defaultDate).AddDays(numberOfDays);
This will use `defaultDate` when `ToDate` has no value, and the value of `ToDate` otherwise.
Problem
I had used `AddDays(x)` method before on `DateTime` objects and it was working fine but now my object is defined like this: ``` public DateTime? To_Date { get; set; } ``` And looks like this one does not have a `AddDays` method. How can I call it then?