这段代码有什么问题 [英] What wrong with this code

查看:99
本文介绍了这段代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime? someDate = null;

someDate = (bool)CheckBoxExpired.IsChecked ? DateTime.Now : null;

if ((bool)CheckBoxExpired.IsChecked) someDate = DateTime.Now; else someDate = null;





第二行代码给出以下错误:



条件表达式的类型无法确定,因为DateTime和Null之间没有隐式转换br />


第三行还可以。



在我看来,两行代码是一样的,但为什么第一个出现错误?



The second line of code gives the following error:

type of conditional expression cannot be determined because there is no implicit conversion between DateTime and Null

The third line is ok.

In my opinion both lines of code are the same, but why do I get an error on the first one?

推荐答案

请查看以下链接。它已经非常明确地解释了这背后的原因。


http://stackoverflow.com/questions/295833/nullable-type-issue-with-conditional-operator [ ^ ]
Please check below link. It has very clearly explained reason behind this.

http://stackoverflow.com/questions/295833/nullable-type-issue-with-conditional-operator[^]


试试这个:

Try this:
? DateTime.Now : (DateTime?) null


这篇关于这段代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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