Possible Duplicates:
Nullable types and the ternary operator. Why won’t this work?
Conditional operator assignment with nullable<value> types?
This will not compile, stating “Type of conditional expression cannot be determined because there is no implicit conversion between ‘System.DateTime’ and ””
task.ActualEndDate = TextBoxActualEndDate.Text != "" ? DateTime.Parse(TextBoxActualEndDate.Text) : null;
This works just fine
if (TextBoxActualEndDate.Text != "")
task.ActualEndDate = DateTime.Parse(TextBoxActualEndDate.Text);
else
task.ActualEndDate = null;
This doesn’t work because the compiler will not insert an implicit conversion on both sides at once, and
nullrequires an implicit conversion to become a nullable type.Instead, you can write
This only requires one implicit conversion (
DateTimetoDateTime?).Alternatively, you can cast either left side:
This also requires only one implicit conversion.