This is a bit off-topic, but bare with me 🙂

My good friend Yaniv just discovered something a bit wierd in C# 2005 regarding Nullable types.

Consider the following piece of code:

DateTime? modified;
modified = row.IsmodifiedNull() ?
null : row.modified;

What it basically does is sets null to “modified” if row.IsmodifiedNull() returns true or sets row.modified into “modified” if row.IsmodifiedNull() returns false.

This code cannot be compiled. The compiler yelled that it cannot set null to “modified” even though modified is defined as a Nullable type.

This code is actually equivalent to the abobe code, doesn’t use the “?” syntax and DOES compile:

if (row.IsmodifiedNull())
modified = null;
modified = row.modified;

After further investigation, Yaniv changed the code to the code below and it did work:

modified = row.IsmodifiedNull() ? (
Nullable<DateTime>)null : row.modified;

Its reasonable to assume that the “?” syntax is eventually translated in compile time into an “if” statement so why in a normal “if” statement setting “modified” to null works and in the “?” syntax it does not.

Bug? Feature?

I wonder who can comment on this.