
myEmployee.age = conditionMet ? someNumber : null;
但以下工作正常:
if(conditionMet){ myEmployee.age = someNumber;}else{ myEmployee.age = null;} 为什么我不能在条件运算符中将值设置为null?我的代码中的所有if语句都不好.
谢谢.
解决方法 双方的类型必须相同(或可隐式兑换):myEmployee.age = conditionMet ? someNumber : (int?)null;
从docs:
总结Either the type of first_Expression and second_Expression must be the same,or an implicit conversion must exist from one type to the other.
以上是内存溢出为你收集整理的C#.net无法为内联if语句中的变量赋值null全部内容,希望文章能够帮你解决C#.net无法为内联if语句中的变量赋值null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)