C#中DateTime的空值 [英] Null value for DateTime in C#
问题描述
我想在C#程序中重置数据库的DateTime值,但似乎无法取消DateTime。有关如何执行此操作或是否如何重置DateTime值的任何建议?在此先感谢。
Hi, i want to reset DateTime values for my database in a C# program but it seems DateTime can't be nulled. Any advice on how to do it or if not how to reset the DateTime value? Thanks in advance.
推荐答案
这是一个结构,一个值类型,因此不可为空。如果使用类型System.DateType?
,则可以使System.DateType
的成员或变量为nullable:
This is a structure, a value type and hence is not nullable. You can make a member or a variable ofSystem.DateType
nullable if you use the typeSystem.DateType?
instead:
System.DateType? myTime = null;
//...
if (myTime == null) //...
请参阅: http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=vs.90%29.aspx [ ^ ]。
在语义上,看起来你补充了任何非可空类型的值集,只有一个额外的点:空值,就是全部。我不建议尝试使用重置值(如 default(System.DateTime)
)。默认值是有效的时间点,不能认为是没时间。不要使用它,更好地使用可空类型。
Please see: http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=vs.90%29.aspx[^].
Semantically, it looks like you complement the value set of any non-nullable type with one extra point: null value, that's all. I don't advise to try to use "reset value" (like default(System.DateTime)
). The default value is a valid point of time, it cannot be considered as "no time". Don't use it, better use the nullable type.
您可以使用
Hi ,
You can send a value to data base using
DateTime.MinValue
谢谢,
Nani。
Thanks,
Nani.
这篇关于C#中DateTime的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!