在获取SQL时,Resharper建议在DateTime为DBNull.Value时使用new DateTime()。我一直使用DateTime.MinValue。哪种方式才是正确的?
DateTime varData = sqlQueryResult["Data"] is DateTime ? (DateTime) sqlQueryResult["Data"] : new DateTime();发布于 2010-03-16 17:20:31
来自the documentation of DateTime.MinValue
MinValue定义分配给未初始化的DateTime变量的日期和时间。
因此,结果日期将是相同的。因为DateTime是一个值类型,所以这两个选项应该是等价的。就我个人而言,我更喜欢编写DateTime.MinValue,因为它是自我记录的。
PS:如果您的数据可以包含(有意义的)空值,那么您可能需要考虑使用nullable types (DateTime?)。
https://stackoverflow.com/questions/2453145
复制相似问题