C# 如何检查默认的 DateTime 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16281621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:37:21  来源:igfitidea点击:

How to check for default DateTime value?

c#.netdesign-patterns

提问by dasheddot

I need to check a DateTimevalue if it has a value or not.

我需要检查一个DateTime值是否有值。

I have several options:

我有几个选择:

if (dateTime == default(DateTime))

or

或者

if (dateTime == DateTime.MinValue)

or using a nullable DateTime?

或使用可空值 DateTime?

if (nullableDateTime.HasValue)

Personally I would prefer the third version, since its pretty good readable. But in our database we have some datetime columns which are defined as not null. So in some cases I have to go with the first two options.

我个人更喜欢第三个版本,因为它的可读性很好。但是在我们的数据库中,我们有一些定义为非空的日期时间列。因此,在某些情况下,我必须采用前两个选项。

I read somewhere that the defaultkeyword should be used when working with generics, but isn't it much more readable in this case? When using the second option I have to know that the default value of a new and empty DateTime instance is DateTime.MinValue which has the smell of an implementation detail for me.

我在某处读到default在使用泛型时应该使用关键字,但在这种情况下它不是更具可读性吗?使用第二个选项时,我必须知道新的空 DateTime 实例的默认值是 DateTime.MinValue,它对我来说具有实现细节的味道。

So which option should I use to use "best practice"?

那么我应该使用哪个选项来使用“最佳实践”?

采纳答案by Jon Skeet

I would probably make this really explicit:

我可能会让这非常明确:

// Or private, or maybe even public
internal static readonly DateTime MagicValueForNullDateTimeInNonNullableColumns
    = DateTime.MinValue;

Okay, so I'd make the name slightlyless wordy, but you get what I mean. It doesn't really matter much how you initialize that value - it's clear what you're trying to do.

好的,所以我会让这个名字稍微不那么冗长,但你明白我的意思。您如何初始化该值并不重要 - 很明显您要尝试做什么。

(Of course, use a nullable type any time you can for this. I'm assuming the question was asked because you couldn'tdo so in certain situations.)

(当然,您可以随时为此使用可空类型。我假设有人问这个问题是因为在某些情况下您不能这样做。)

回答by LukeH

I need to check a DateTimevalue if it has a value or not.

我需要检查一个DateTime值是否有值。

You've pretty-much described the textbook situation requiring a Nullable<DateTime>. I'd go with that option.

您已经几乎描述了需要Nullable<DateTime>. 我会选择那个选项。

(You'd obviously need some sort of translation layer if you need to persist the values to a non-null db column, but I'd try to keep any "magic" value as close to the db as possible, and try to keep your C# code clean and idiomatic.)

(如果您需要将值保留到非空 db 列,您显然需要某种转换层,但我会尽量使任何“魔术”值尽可能接近 db,并尽量保持您的 C# 代码干净且符合习惯。)

回答by Igal Tabachnik

The "best" practice would be using the nullable DateTime. Nullable value types were created exactlyfor that reason of not trusting "magic" numbers.

“最佳”做法是使用 nullable DateTime。可空值类型正是出于不信任“魔术”数字的原因而创建的。

What's most important about this approach is its intent - what does it meanin your context to have a "default" date/time?

这种方法最重要的是它的意图 -在您的上下文中具有“默认”日期/时间意味着什么?

回答by Kuzgun

Maybe you can set a default value in your database with getdate() and then update all null values to min value or whatever you like. At least you will have one condition

也许您可以使用 getdate() 在您的数据库中设置一个默认值,然后将所有空值更新为最小值或您喜欢的任何值。至少你会有一个条件