C# 检查 DateTime 变量是否已分配值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/305154/
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-03 22:27:22  来源:igfitidea点击:

Checking to see if a DateTime variable has had a value assigned

c#datetime

提问by Moo

Is there an easy way within C# to check to see if a DateTime instance has been assigned a value or not?

C# 中是否有一种简单的方法来检查 DateTime 实例是否已分配值?

采纳答案by Jon Skeet

The only way of having a variable which hasn't been assigned a value in C# is for it to be a local variable - in which case at compile-time you can tell that it isn't definitely assigned by trying to read from it :)

在 C# 中拥有一个没有被赋值的变量的唯一方法是让它成为一个局部变量 - 在这种情况下,在编译时你可以通过尝试读取它来判断它不是绝对赋值的: )

I suspect you really want Nullable<DateTime>(or DateTime?with the C# syntactic sugar) - make it nullto start with and then assign a normal DateTimevalue (which will be converted appropriately). Then you can just compare with null(or use the HasValueproperty) to see whether a "real" value has been set.

我怀疑你真的想要Nullable<DateTime>(或DateTime?使用 C# 语法糖) - 让它null开始,然后分配一个正常值DateTime(将被适当转换)。然后您可以与null(或使用HasValue属性)进行比较以查看是否已设置“真实”值。

回答by baretta

Use Nullable<DateTime>if possible.

Nullable<DateTime>如果可能,请使用。

回答by TcKs

DateTime is value type, so it can not never be null. If you think DateTime? ( Nullable ) you can use:

DateTime 是值类型,所以它不能永远为空。如果你认为DateTime?( Nullable ) 你可以使用:

DateTime? something = GetDateTime();
bool isNull = (something == null);
bool isNull2 = !something.HasValue;

回答by Hath

do you mean like so:

你的意思是这样吗:

DateTime datetime = new DateTime();

if (datetime == DateTime.MinValue)
{
    //unassigned
}

or you could use Nullable

或者你可以使用 Nullable

DateTime? datetime = null;

 if (!datetime.HasValue)
 {
     //unassigned
 }

回答by Steve Dunn

I generally prefer, where possible, to use the default value of value types to determine whether they've been set. This obviously isn't possible all the time, especially with ints - but for DateTimes, I think reserving the MinValue to signify that it hasn't been changed is fair enough. The benefit of this over nullables is that there's one less place where you'll get a null reference exception (and probably lots of places where you don't have to check for null before accessing it!)

在可能的情况下,我通常更喜欢使用值类型的默认值来确定它们是否已设置。这显然不是一直可能的,尤其是对于整数 - 但对于 DateTimes,我认为保留 MinValue 以表示它没有改变就足够了。与可空值相比,这样做的好处是少了一个地方,你会得到一个空引用异常(并且可能有很多地方你不必在访问它之前检查空值!)

回答by Arcturus

I just found out that GetHashCode() for an unassigned datetime is always zero. I am not sure if this is a good way to check for null datetime, because, I can't find any documentation on why this behavior is displayed.

我刚刚发现未分配日期时间的 GetHashCode() 始终为零。我不确定这是否是检查空日期时间的好方法,因为我找不到任何关于为什么会显示这种行为的文档。

if(dt.GetHashCode()==0)
{
    Console.WriteLine("DateTime is unassigned"); 
} 

回答by Sabine

I'd say the default value is always new DateTime(). So we can write

我会说默认值总是new DateTime(). 所以我们可以写

DateTime datetime;

if (datetime == new DateTime())
{
    //unassigned
}

回答by sonatique

put this somewhere:

把它放在某个地方:

public static class DateTimeUtil //or whatever name
{
    public static bool IsEmpty(this DateTime dateTime)
    {
        return dateTime == default(DateTime);
    }
}

then:

然后:

DateTime datetime = ...;

if (datetime.IsEmpty())
{
    //unassigned
}

回答by Urasquirrel

If you don't want to have to worry about Null value issues like checking for null every time you use it or wrapping it up in some logic, and you also don't want to have to worry about offset time issues, then this is how I solved the problem:

如果您不想担心 Null 值问题,例如每次使用时检查 null 或将其包装在某些逻辑中,并且您也不想担心偏移时间问题,那么这就是我是如何解决问题的:

startDate = startDate < DateTime.MinValue.AddDays(1) ? keepIt : resetIt

I just check that the defaulted value is less than a day after the beginning of time. Works like a charm.

我只是检查默认值是否在时间开始后不到一天。奇迹般有效。