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 联系我们: