How to set DateTime to null

前端 未结 6 1354
半阙折子戏
半阙折子戏 2021-01-31 13:25

Using C#. I have a string dateTimeEnd.

If the string is in right format, I wish to generate a DateTime and assign it to eventCustom.DateTimeEnd

6条回答
  •  你的背包
    2021-01-31 14:22

    This line:

    eventCustom.DateTimeEnd = dateTimeEndResult = true ? (DateTime?)null : dateTimeEndResult;
    

    is same as:

    eventCustom.DateTimeEnd = dateTimeEndResult = (true ? (DateTime?)null : dateTimeEndResult);
    

    because the conditional operator ? has a higher precedence than the assignment operator =. That's why you always get null for eventCustom.DateTimeEnd. (MSDN Ref)

提交回复
热议问题