本文介绍了Nullable DateTime是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请检查以下代码
抛出 Nullable< T> .GetType 这是因为 Object.Equals , Object.GetHashCode 和 Object.ToString 被覆盖为 Nullable< T> ,但是 Object.GetType 不是(因为它不能它没有标记为 virtual )。Please check following code
DateTime? tmp = new DateTime(); tmp = null; return tmp.ToString();It returns 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
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).
这篇关于Nullable DateTime是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!