c# - 使用 DateTime.MinValue 还是可以为 null 的 DateTime 更好?

标签 c# datetime

如果我在名为“TimeLastAccessed”的类上有一个 DateTime,那么这个 DateTime 是否可以为 null 更有意义:

public DateTime? TimeLastAccessed { get; set }

if (TimeLastAccessed == null) // ... handle it

表明它从未被访问过或检查 DateTime.MinValue

public DateTime TimeLastAccessed { get; set; }

if (TimeLastAccessed == DateTime.MinValue) // ... handle it

?

最佳答案

使用 Nullable 更有意义。这就是 Nullable 的想法——表示值类型没有有效值。使用 MinValue 是针对没有 Nullable 的情况的补丁。

关于c# - 使用 DateTime.MinValue 还是可以为 null 的 DateTime 更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2144473/

相关文章:

c# - 在 C# View 模型中访问 XAML 对象

c# - 如何处理 Multi-Tenancy ASP MVC 站点的路径设置

mysql - 查询日期 : is "dateval LIKE ' 2014-01-01 %'" a best practice?

php - 如何获取给定城市的时间?

c# - 如何从具有当前纬度和经度的 cosmos db 查找最近的优惠券

c# - 为什么更新结构数组比更新类数组更快?

c# - .net 中任务的说明

java - 如何将sql中的日期时间格式转换为java中的日期时间[eclipse]

python - 如何从四分之一字符串推断日期时间?

bash - 在 bash 中创建将运行特定时间的 while 循环