只是一个虚构的代码,但为什么这行不通? (因为 date
变量可以为空)
DateTime? date = textBoxDate.Text != "" ? textBoxDate.Text : null;
错误是“System.DateTime
和 <null>
之间没有显式转换
最佳答案
试试这个:
DateTime? date = String.IsNullOrEmpty(textBoxDate.Text) ?
null as DateTime? : DateTime.Parse(textBoxDate.Text);
关于c# - 在单行条件中传递空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3942779/