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

我怕爱的太早我们不能终老 提交于 2019-11-27 16:06:41

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.

itowlson

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;
                                  ^^^^^^^^^^^
Eric Lippert

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.

The reason is that null is of type object so you have to cast it to the correct type, like this:

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

The most correct way (IMO) is to do this

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

I use the null collaescing operator frequently in this manner.

This is the error probably which you get in this situation:

error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'int')

The compiler is is explaining that it does not know how convert null into a DateTime.


Fix:

you need to cast explicitly the expression which may return null to the nullable type. This will work

((DateTime?) null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!