DateTime.MinValue vs C#中的新DateTime() [英] DateTime.MinValue vs new DateTime() in C#

查看:135
本文介绍了DateTime.MinValue vs C#中的新DateTime()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当获取SQL DateTime Resharper建议在值为 DBNull.Value 时使用新的DateTime()。我一直使用 DateTime.MinValue 。这是正确的方法?

When getting SQL DateTime Resharper suggests to use new DateTime() when value is DBNull.Value. I've always used DateTime.MinValue. Which is the proper way?

DateTime varData = sqlQueryResult["Data"] is DateTime ? (DateTime) sqlQueryResult["Data"] : new DateTime();


推荐答案

DateTime.MinValue的文档


MinValue定义分配给未初始化的DateTime变量的日期和时间。

MinValue defines the date and time that is assigned to an uninitialized DateTime variable.

因此,日期会一样。由于 DateTime 是一个值类型,所以这两个选项都应该是等效的。个人而言,我喜欢写$ DateTime.MinValue ,因为它是自我记录的。

Thus, the resulting date will be the same. Since DateTime is a value type, both options should be equivalent. Personally, I prefer to write DateTime.MinValue, since it's self-documenting.

PS:你可能想请考虑使用可空类型 DateTime?),如果您的数据可以包含(有意义)的空值。

PS: You might want to consider using nullable types (DateTime?), if your data can contain (meaningful) null values.

这篇关于DateTime.MinValue vs C#中的新DateTime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆