0

Why doesn't this work?

DateTime? date = condition?DateTime.Now: null; //Error: no implicit conversion between DateTime and null

While this does?

DateTime? date;
if (condition)
{
  date = DateTime.Now;
}
else
  date = null;

A somewhat similar problem can be found here, but I just cannot make the association. Thanks for your help..

Update: I read the spec document recommended by Jon Skeet, and it says:

If one of the second and third operands is of the null type and the type of the other is a 
reference type, then the type of the conditional expression is that reference type

So, when a ternary operator is used, a conversion is forced, even though I've specified the variable type?

Community
  • 1
  • 1
JustAPup
  • 1,720
  • 12
  • 19
  • 1
    I'm tempted to close this as a duplicate. See [this](http://stackoverflow.com/questions/858080/nullable-types-and-the-ternary-operator-why-is-10-null-forbidden), [this](http://stackoverflow.com/questions/75746/conditional-operator-assignment-with-nullablevalue-types), or [this](http://stackoverflow.com/questions/295833/nullable-type-issue-with-conditional-operator). – Mike Zboray Dec 23 '15 at 20:55
  • 1
    Your "similar problem" is for java. That isn't really relevant although the languages are superficially similar. You want the [C# language spec](https://www.microsoft.com/en-us/download/details.aspx?id=7029) not the java spec. – Mike Zboray Dec 23 '15 at 21:01
  • mike z, I realize that you are right.. What is the appropriate thing to do here? Should I delete this question? – JustAPup Dec 23 '15 at 21:07
  • If your question has been answered by those other ones then deleting is fine. If it has not been answered, modify it to call out what you do not understand (you may wish to look at section 7.14 of the C# spec). If you cannot delete, let me know and I can mark as duplicate. – Mike Zboray Dec 23 '15 at 21:12

2 Answers2

6

Why doesn't this work?

To answer this, I'll quote the error message:

no implicit conversion between DateTime and null

DateTime.Now is of type DateTime. null is not a valid DateTime value. The compiler is saying that it cannot find any common type that both DateTime.Now and null could be implicitly (because you did not explicitly specify any conversion) converted to.

However, for the ternary ?: operator in C#, it is mandatory that the second and the third operand can be converted to the same type, which is also the type of the return value of the whole expression involving ?:

Apparently, you want to retrieve a DateTime? value - DateTime values are not implicitly converted to DateTime?, but you can do so explicitly:

DateTime? date = condition ? (DateTime?)DateTime.Now : null;

This way, you are telling the compiler to consider the second operand to be of type DateTime?. The third operand, null, can be implicitly converted to DateTime?, so there is no contradiction any more and hence the compiler error disappears.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
0

This doesn't work because the expression condition?DateTime.Now: null cannot be evaluated because its type is unclear. The expression must be evaluated before the assignment, and the trouble isn't with the assignment; it's with the evaluation of the expression. That's why the if-then works and the ternary operator doesn't.

adv12
  • 8,443
  • 2
  • 24
  • 48