可为空的DateTime工作是否正确? [英] Is Nullable DateTime work correct?

查看:105
本文介绍了可为空的DateTime工作是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查下面的代码

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 the HasValue property is true, or an empty string ("") if the HasValue 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆