检查DateTime变量是否已分配值 [英] Checking to see if a DateTime variable has had a value assigned

查看:102
本文介绍了检查DateTime变量是否已分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

在C#中有一个简单的方法来检查一个DateTime实例是否被赋值>在C#中没有赋值的变量的唯一方法是将其作为局部变量 - 在这种情况下,在编译时,您可以通过尝试从中读取它来确定它不是绝对分配的我想你真的想要 Nullable< DateTime> (或 DateTime?



/ code>与C#语法糖) - 使它 null 开始,然后分配一个正常的 DateTime 值(将被适当转换)。然后,您可以与 null (或使用 HasValue 属性)进行比较,以查看是否已经有真实值设置。


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

解决方案

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 :)

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

这篇关于检查DateTime变量是否已分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆