DateTime.MinValue与C#中的新DateTime()

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


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?

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

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

6 个解决方案

#1


18  

From the documentation of DateTime.MinValue:

从DateTime.MinValue的文档:

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

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

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.

因此,结果日期将是相同的。由于DateTime是值类型,因此两个选项应该是等效的。就个人而言,我更喜欢写DateTime.MinValue,因为它是自我记录的。

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

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

#2


2  

Usually, I would handle the NULL case explicitly. Something along these lines:

通常,我会明确处理NULL情况。这些方面的东西:

if (!sqlQueryResults.IsNull("Data"))
    DoComputationDependantOnDateTime((DateTime) sqlQueryResults["Data"]);

In some cases, it might make sense to perform the logic even if no data is present. In such cases, only you know what default value to start from. Likely candidates:

在某些情况下,即使没有数据,执行逻辑也是有意义的。在这种情况下,只有您知道从哪个默认值开始。可能的候选人:

  • DateTime.MinValue
  • DateTime.MinValue
  • DateTime.MaxValue
  • DateTime.MaxValue
  • DateTime.Now
  • DateTime.Now

I would never use new DateTime() as it has very poor readability. What does that statement return? You (and future developers on the project) might have to look into the documentation to figure out what the value is initialized to.

我永远不会使用新的DateTime(),因为它的可读性非常差。该声明的回报是什么?您(以及该项目的未来开发人员)可能必须查看文档以确定初始化值的内容。

#3


1  

I would say to always use MinValue because then you know exactly what value is in it. When you simply use new DateTime() you are initializing it to hold a value, but not specifying what value to place into it. It is also a lot cleaner and clearer to explicitly put a value into it.

我会说总是使用MinValue因为那时你确切地知道它的价值。当您只是使用新的DateTime()时,您正在初始化它以保存一个值,但不指定要放入的值。明确地将值放入其中也更清晰,更清晰。

#4


1  

Default value of DateTime is the DateTime.MinValue, so I guess it doesn't matter which of these you use ;)

DateTime的默认值是DateTime.MinValue,所以我猜你使用哪一个并不重要;)

#5


0  

There is no "proper" way. There is a convention that people set up and that the team follows.

没有“正确”的方式。人们建立了一个公约,团队遵循这个惯例。

DateTime.MinValue and new DateTime () are valid definitions for a null, if the developer in charge determines so.

DateTime.MinValue和new DateTime()是null的有效定义,如果开发人员确定如此。

#6


0  

The most maintainable way of representing a null value would be to use a Nullable<DateTime> i.e. DateTime?. That way the null value is clearly represented as a non-value instead of a magic value that needs special treatment.

表示空值的最可维护方式是使用Nullable 即DateTime?。这样,空值清楚地表示为非值而不是需要特殊处理的魔法值。

(The null value of course also needs special treatment, but it's much harder to overlook.)

(当然,空值也需要特殊处理,但要忽略它。)

If you use a magic value to represent null values, no value is much better than any other. You can use DateTime.MinValue or new DateTime() (which give the same result), or you can use any arbitrary value that is outside the range that your application is using.

如果使用魔术值来表示空值,则没有任何值比其他任何值更好。您可以使用DateTime.MinValue或新的DateTime()(它们给出相同的结果),或者您可以使用任何超出应用程序使用范围的任意值。

A value like DateTime.MinValue has a slight advantage as it's already defined as a constant, on the other hand you can define your own constant that has a slightly better name, for example:

像DateTime.MinValue这样的值有一点点优势,因为它已经被定义为常量,另一方面你可以定义自己的常量,它具有稍微好一点的名称,例如:

public const DateTime DateThatMeansNull = new DateTime(1685, 3, 21);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/03/16/b031f59e5861abe93f0b5b429423aa75.html



 
  © 2014-2022 ITdaan.com 联系我们: