Cast the null literal: (DateTime?)null
or (Nullable<DateTime>)null
.
You can also use default(DateTime?)
or default(Nullable<DateTime>)
And, as other answers have noted, you can also apply the cast to the DateTime value rather than to the null literal.
EDIT (adapted from my comment to Prutswonder's answer):
The point is that the conditional operator does not consider the type of its assignment target, so it will only compile if there is an implicit conversion from the type of its second operand to the type of its third operand, or from the type of its third operand to the type of its second operand.
For example, this won't compile:
bool b = GetSomeBooleanValue();
object o = b ? "Forty-two" : 42;
Casting either the second or third operand to object
, however, fixes the problem, because there is an implicit conversion from int to object and also from string to object:
object o = b ? "Forty-two" : (object)42;
or
object o = b ? (object)"Forty-two" : 42;