我有打算使用两个DateTimeOffset?
变量中最大的代码。我一直在使用以下代码进行此操作:
return firstTimestamp > secondTimestamp ? firstTimestamp : secondTimestamp
但是,如MSDN文档所述,如果两个值中的任何一个为null,则该操作始终解析为False
。这意味着,如果某个值位于firstTimestamp
中,并且secondTimestamp
为null,则在应返回firstTimestamp
的地方返回null。
是否存在一种有效的简化方法来比较空值?我能想到的唯一解决方案是:
if(firstTimestamp.HasValue && secondTimestamp.HasValue){
return firstTimestamp > secondTimestamp ? firstTimestamp : secondTimestamp;
}else if(firstTimestamp.HasValue){
return firstTimestamp;
}
return secondTimestamp;