From ?: Operator
:
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
condition ? first_expression : second_expression;
Convert.ToDateTime
returns DateTime
and there is no implicit conversion between null
and DateTime
. And the conditional operator is an expression and that needs a return type.
One option seems logical to me using DateTime.TryParse
(which returns boolean
) as a first expression and use another boolean (true
or false
) as a second expression. Damiths' answer seems logical.
Or instead you can use nullable DateTime like DateTime?
DateTime? foo;
if(foo.HasValue)
{
//Nullable DateTime has a value..
}