[英]Is Nullable DateTime work correct?
Please check following code 请检查以下代码
DateTime? tmp = new DateTime();
tmp = null;
return tmp.ToString();
It returns String.Empty
. 它返回
String.Empty
。
Is it correct? 这是对的吗?
May be it will be better to rise exception in second line of code 可能是在第二行代码中引发异常会更好
Yes, it's correct. 是的,这是对的。 From the documentation
从文档中
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.的电流的值的文本表示
Nullable<T>
对象,如果HasValue
属性为true,或空字符串(""
),如果HasValue
属性为false。
Note also that Nullable<T>.Equals
and Nullable<T>.GetHashCode
do not throw in this case but that Nullable<T>.GetType
does throw. 另请注意,
Nullable<T>.Equals
和Nullable<T>.GetHashCode
不会抛出这种情况,但Nullable<T>.GetType
会抛出。 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
). 这是因为
Nullable<T>
会覆盖Object.Equals
, Object.GetHashCode
和Object.ToString
,但Object.GetType
不会被覆盖(因为它不能被标记为virtual
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.