Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the conditional operator correctly allow the use of "null" for assignment to nullable types? [duplicate]

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;
like image 289
Daniel Coffman Avatar asked Mar 15 '10 22:03

Daniel Coffman


3 Answers

This doesn't work because the compiler will not insert an implicit conversion on both sides at once, and null requires an implicit conversion to become a nullable type.

Instead, you can write

task.ActualEndDate = TextBoxActualEndDate.Text != "" ? 
    DateTime.Parse(TextBoxActualEndDate.Text) : new DateTime?();

This only requires one implicit conversion (DateTime to DateTime?).

Alternatively, you can cast either left side:

task.ActualEndDate = TextBoxActualEndDate.Text != "" ? 
    (DateTime?)DateTime.Parse(TextBoxActualEndDate.Text) : null;

This also requires only one implicit conversion.

like image 175
SLaks Avatar answered Nov 05 '22 08:11

SLaks


The conditional operator doesn't look at what the value is being returned into. It only looks at the values it's being asked to choose between: a DateTime and null. It can't identify these as instances of the same type (because null isn't a valid DateTime), hence the error. You and I know that Nullable<DateTime> could do the job, but the conditional operator isn't allowed to introduce "larger" types: it's only allowed to look at the types of the two expressions it's choosing between. (Thanks to Aaronaught in comments for clarification of this point and a nice clarifying example.)

To work around this, give the operator a hint by casting the DateTime:

TextBoxActualEndDate.Text != "" ? (DateTime?)(DateTime.Parse(TextBoxActualEndDate.Text)) : null;
                                  ^^^^^^^^^^^
like image 43
itowlson Avatar answered Nov 05 '22 10:11

itowlson


This is a duplicate of

Nullable types and the ternary operator: why is `? 10 : null` forbidden?

My answer to

Conditional operator cannot cast implicitly?

gives an analysis that is germane to this question.

I'll also be blogging about a similar issue with the conditional operator in April; watch the blog for details.

like image 1
Eric Lippert Avatar answered Nov 05 '22 09:11

Eric Lippert