Nullable type issue with ?: Conditional Operator

The compiler is telling you that it doesn’t know how convert null into a DateTime.

The solution is simple:

DateTime? foo;
foo = true ? (DateTime?)null : new DateTime(0);

Note that Nullable<DateTime> can be written DateTime? which will save you a bunch of typing.

Leave a Comment