Nullable type issue with ?: Conditional Operator

C#GenericsNullableConditional Operator

C# Problem Overview


Could someone explain why this works in C#.NET 2.0:

	Nullable<DateTime> foo;
	if (true)
		foo = null;
	else
		foo = new DateTime(0);

...but this doesn't:

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

The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'."

Not that I can't use the former, but the second style is more consistent with the rest of my code.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

FYI (Offtopic, but nifty and related to nullable types) we have a handy operator just for nullable types called the null coalescing operator

??

Used like this:

// Left hand is the nullable type, righthand is default if the type is null.
Nullable<DateTime> foo;
DateTime value = foo ?? new DateTime(0);

Solution 3 - C#

It's because in a ternary operator, the two values must resolve to the same type.

Solution 4 - C#

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo;
foo = true ? default(DateTime) : new DateTime(0);

Example usage with the current accepted answer:

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

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value and no error will be encountered. Example:

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

Solution 5 - C#

I know this question was asked in 2008 and it is now 5 years later but the answer marked as an answer does not satisfy me. The real answer is that DateTime is a struct, and as a struct it is not compatible with null. You have two ways of solving that:

First is to make null compatible with DateTime (for instance, cast null to DateTime? as the gentleman with 70 upvotes suggests, or cast null to Object or to ValueType).

The second is to make the DateTime compatible with null (for instance, cast DateTime to DateTime?).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNick GotchView Question on Stackoverflow
Solution 1 - C#Stewart JohnsonView Answer on Stackoverflow
Solution 2 - C#FlySwatView Answer on Stackoverflow
Solution 3 - C#MojoFilterView Answer on Stackoverflow
Solution 4 - C#newfurnitureyView Answer on Stackoverflow
Solution 5 - C#MishaxView Answer on Stackoverflow