I'm working with a nullable DateTime object and ran into some strange behavior. Here's a sample function:
public DateTime? Weird()
{
DateTime check = DateTime.Now;
DateTime? dt;
if (check == DateTime.MinValue)
dt = null;
else
dt = Viewer.ActiveThroughUTC.ToLocalTime();
//this line give a compile error
dt = (check == DateTime.MinValue) ? (null) : (Viewer.ActiveThroughUTC.ToLocalTime());
return dt;
}
As far as I know, the line with the ternary operator should be the same as the preceding four lines, but VS2010 is giving me a compile error, saying that no conversion exists between <null>
and DateTime (even though the object in question is a 'DateTime?'). Is there something I should know about the ternary operator or is this (gasp?) a bug?