C#中的默认日期为null [英] Default Date to null in C#
问题描述
大家好,
我尝试了以下操作来取消默认日期,但没有结果。有什么问题。
if(exg.CertficateDatetime ==(01/01/1753))
{
(exg.CertficateDatetime)= null;
}
提前致谢。
Hi All,
I tried the following to null the default date, but no result. What is wrong in it.
if (exg.CertficateDatetime == ("01/01/1753"))
{
(exg.CertficateDatetime) = null;
}
Thanks in advance.
推荐答案
DateTime是一个值类型,所以如果你想设置null值datetime提交了它抛出错误。
有一些解决方法是:
Hi,
DateTime is a value type so if you want to set null value in datetime filed it throw error.
there are some work around that's is :
DateTime? MyDate=null;
或
Or
Nullable<datetime> MyDate=null;</datetime>
它们与每个CLR相同。
They are same as per CLR.
类型System.DateTime
不是引用类型,但是值类型:
http://msdn.microsoft.com/en-us/ library / system.datetime.aspx [ ^ ]。
要获得可空类型,请使用类型System.DateTime?
:
The typeSystem.DateTime
is not a reference type but is a value type:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].
To have a nullable type, use the typeSystem.DateTime?
:
System.DateTime? referenceTime = null;
//...
referenceTime = System.DateTime.Now; // not null
SystemDateTime valueTime = time.Value; // assignment between objects of value and reference types
请参阅:
http: //msdn.microsoft.com/en-us/library/1t3y8s4s.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/vstudio/2cf62fcy%28v=vs.120%29.aspx [ ^ ]。 />
Please see:
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/2cf62fcy%28v=vs.120%29.aspx[^].
这篇关于C#中的默认日期为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!