可为空的DateTime工作是否正确? [英] Is Nullable DateTime work correct?
问题描述
请检查下面的代码
DateTime? tmp = new DateTime();
tmp = null;
return tmp.ToString();
它返回的String.Empty
。
是不是正确的?
可能会更好上涨异常的代码第二行
May be it will be better to rise exception in second line of code
推荐答案
是的,这是正确的。从文档
Yes, it's correct. From the documentation
目前可空<的值的文本表示; T>
对象,如果的HasValue
属性真的,还是一个空字符串(,
)如果的HasValue
属性设置为false。
The text representation of the value of the current
Nullable<T>
object if theHasValue
property is true, or an empty string (""
) if theHasValue
property is false.
还要注意的是可空< T> .Equals
和可空< T&取代。 GetHashCode的
不要在这种情况下,但可空<抛出; T> .GetType
的确实的罚球。这是因为的Object.Equals
, Object.GetHashCode
和 Object.ToString
被覆盖了可空< T>
但 Object.GetType
不是(因为它不能像它没有标记为虚拟
)。
Note also that Nullable<T>.Equals
and Nullable<T>.GetHashCode
do not throw in this case but that Nullable<T>.GetType
does throw. This is because Object.Equals
, Object.GetHashCode
and Object.ToString
are overridden for Nullable<T>
but that Object.GetType
is not (because it can not be as it is not marked as virtual
).
这篇关于可为空的DateTime工作是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!