Nullable DateTimes and the AddDays() extension

前端 未结 4 1136
暗喜
暗喜 2021-01-12 02:34

I have a DateTime variable that can either be null or a Datetime. I figured a nullable DateTime type would work, but I am getting an error telling me that said

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 03:02

    You need to go through the "Value" property:

    lastInvite.Value.AddDays(7)
    

    Note that this will throw an exception if the DateTime is actually null. Luckily, there is another property you can use to test for this, "HasValue".

    if (lastInvite.HasValue){ /* code */ }
    

提交回复
热议问题