本文介绍了日期时间"零"值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了很多,但无法找到一个解决方案。你如何处理一个DateTime应该能够包含未初始化值(相当于空)?我有可能有一个DateTime属性值设置与否的一类。我在想初始化财产持有人DateTime.MinValue,然后可以很容易地进行检查的。我想这是一个很常见的问题,你是怎么做到的?

I've been searching a lot but couldn't find a solution. How do you deal with a DateTime that should be able to contain an uninitialized value (equivalent to null)? I have a class which might have a DateTime property value set or not. I was thinking of initializing the property holder to DateTime.MinValue, which then could easily be checked. I guess this is a quite common question, how do you do that?

推荐答案

有关正常DateTime是否,如果你不初始化他们都那么他们将匹配 DateTime.MinValue 的,因为它是一个值类型,而不是一个引用类型。

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue, because it is a value type rather than a reference type.

您还可以使用可空的日期时间,像这样的:

You can also use a nullable DateTime, like this:

DateTime? MyNullableDate;

或者更长的形式:

Or the longer form:

Nullable<DateTime> MyNullableDate;

最后,还有一个内置的方式来引用任何类型的默认。这将返回对于引用类型,但对于我们的DateTime例如,它会返回相同的 DateTime.MinValue

And, finally, there's a built in way to reference the default of any type. This returns null for reference types, but for our DateTime example it will return the same as DateTime.MinValue:

default(DateTime)

这篇关于日期时间&QUOT;零&QUOT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:28