我尝试使用以下示例代码?b:c表达式:
DateTime? GetValue(string input)
{
DateTime? val = string.IsNullOrEmpty(input) ? null : DateTime.Parse(input);
return val;
}
Run Code Online (Sandbox Code Playgroud)
我有编译错误,因为在a?b:c表达式因为b和c是不同的数据类型; 不确定我是否可以使用(DateTime?)案例来分享?
DateTime? val = string.IsNullOrEmpty(input) ? null : (DateTime?) DateTime.Parse(input);
Run Code Online (Sandbox Code Playgroud)
我宁愿不使用if将这一个分成两个或三个语句.
Tim*_*ter 11
return string.IsNullOrEmpty(input) ? (DateTime?)null : DateTime.Parse(input);
//or
return string.IsNullOrEmpty(input) ? null : (DateTime?)DateTime.Parse(input);
Run Code Online (Sandbox Code Playgroud)
要么工作,你必须在两种类型之间提供一些兼容性的方法,因为DateTime不能为null,你需要明确地使用你想要去的那个DateTime?,然后编译器可以隐式地转换另一个.
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |