I need to check a DateTime
value 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.
我个人更喜欢第三个版本,因为它的可读性非常好。但是在我们的数据库中,我们有一些datetime列,它们被定义为非null。所以在某些情况下我必须选择前两个选项。
I read somewhere that the default
keyword 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"?
那么我应该使用哪个选项来使用“最佳实践”?
4 个解决方案
#1
44
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 slightly less 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't do so in certain situations.)
(当然,在任何时候都可以使用可空类型。我假设问题被问到,因为在某些情况下你不能这样做。)
#2
14
I need to check a
DateTime
value 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
(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#代码干净且惯用。)
#3
4
The "best" practice would be using the nullable DateTime
. Nullable value types were created exactly for that reason of not trusting "magic" numbers.
“最佳”做法是使用可以为空的DateTime。由于不信任“魔术”数字的原因,创建了可以为空的值类型。
What's most important about this approach is its intent - what does it mean in your context to have a "default" date/time?
这种方法最重要的是它的意图 - 在你的上下文中具有“默认”日期/时间意味着什么?
#4
0
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()在数据库中设置默认值,然后将所有空值更新为最小值或任何您喜欢的值。至少你会有一个条件
#1
44
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 slightly less 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't do so in certain situations.)
(当然,在任何时候都可以使用可空类型。我假设问题被问到,因为在某些情况下你不能这样做。)
#2
14
I need to check a
DateTime
value 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
(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#代码干净且惯用。)
#3
4
The "best" practice would be using the nullable DateTime
. Nullable value types were created exactly for that reason of not trusting "magic" numbers.
“最佳”做法是使用可以为空的DateTime。由于不信任“魔术”数字的原因,创建了可以为空的值类型。
What's most important about this approach is its intent - what does it mean in your context to have a "default" date/time?
这种方法最重要的是它的意图 - 在你的上下文中具有“默认”日期/时间意味着什么?
#4
0
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()在数据库中设置默认值,然后将所有空值更新为最小值或任何您喜欢的值。至少你会有一个条件