简体   繁体   English

Nullable DateTime是否正常工作?

[英]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 the HasValue property is true, or an empty string ( "" ) if the HasValue 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>.EqualsNullable<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.EqualsObject.GetHashCodeObject.ToString ,但Object.GetType不会被覆盖(因为它不能被标记为virtual )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM