加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – Nullable类型,如果问题

发布时间:2020-12-17 07:25:36 所属栏目:百科 来源:网络整理
导读:这是最简单的代码 Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),Nothing,New DateTime(2018,3,20)) 为什么变量testInvoiceDate不是Nothing,而是#1/1 / 0001 12:00:00 AM#?! 这很奇怪! 解决方法 If语句将为两种情况返回相同的数据
这是最简单的代码

Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),Nothing,New DateTime(2018,3,20))

为什么变量testInvoiceDate不是Nothing,而是#1/1 / 0001 12:00:00 AM#?!
这很奇怪!

解决方法

If语句将为两种情况返回相同的数据类型.
因为False-case中的return-type是DateTime,所以return-type是True-case的DateTime-default-value.

DateTime的默认值是DateTime.MinValue,即#1/1/0001 12:00:00 AM#.

这将按预期工作:

Dim testInvoiceDate As DateTime? = If(String.IsNullOrEmpty(Nothing),New DateTime?(New Date(2018,20)))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读