我正在使用一个可为空的 DateTime 对象并遇到了一些奇怪的行为。这是一个示例函数:
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;
}
据我所知,三元运算符的行应该与前面四行相同,但是VS2010给我一个编译错误,说<null>
和DateTime之间不存在转换(即使有问题的对象是'DateTime ?')。关于三元运算符,我应该知道些什么,或者这(喘气?)是一个错误?