In my app, article.DatePublished is a nullable DateTime field. Now I have this following code:
list.Add(article.DatePublished.Ticks);
Here I am getting a compile error as Ticks property does not work with nullable DateTimes.
One way of handling this is:
if (article.DatePublished != null)
list.Add(((DateTime)article.DatePublished).Ticks);
This works, but is this an elegant solution? Or can we make it “better”?
Thanks,
Vivek
You need to get at the
.Valueproperty of theDateTime?.You could otherwise use
nullableDate.GetValueOrDefault().Ticks, which would normalize a null date into the default value ofDateTime, which isDateTime.MinValue.