Nullable DateTimes and the AddDays() extension

前端 未结 4 1162
暗喜
暗喜 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:09

    There are multiple ways to resolve this error.

    1. Use Convert.ToDateTime() method which handles null datetime values.

      Convert.ToDateTime(lastInvite).AddDays(7);

    2. Use Value property

      lastInvite.Value.AddDays(7)

    3. Use GetValueOrDefault() method

      lastInvite.GetValueOrDefault().AddDays(7)

提交回复
热议问题