c# - Issue with setting a DateTime? to null using ternary operator -
possible duplicate:
type result conditional operator in c#
i working on pop-up modifying user, when ran following issue while attempting modify user's date of death :
in scenario, property death
user datetime?
(nullable date time).
user.death = (model.death != null) ? datetime.parse(model.death) : null;
so figured able check if value contained in model (model.death string
) contained value, set date value, otherwise set null, demonstrated above.
however, unable use syntax, not allow me explicitly set user.death
null, using ternary operator, although user.death = null
worked fine.
solution used :
replaced : null
: new nullable<datetime>()
i guess wondering, why unable explicitly set nullable datetime property null using ternary operator?
this doesn't work because compiler not insert implicit conversion on both sides @ once.
you want compiler convert datetime
value datetime?
on 1 side, , null
datetime?
on other side.
cannot happen.
if explicitly convert either half datetime?
, other half implicitly convert too.
Comments
Post a Comment