项目中应用条件运算符?报错:只有 assignment、call、increment、decrement、await 和 new 对象表达式可用作语。
写了个最简单的测试代码,问题依旧:
int i = 0;
string str = "";
i == 0 ? str = "true" : str = "false";
编译照样报错:只有 assignment、call、increment、decrement、await 和 new 对象表达式可用作语。
查看MSDN也不明白,请老师们指点下迷津,谢啦!
str = (i == 0 ? "true" : "false");
我的实际代码是这样的(处理RadioButton选项):
cl.ChildNodes[1].ChildNodes[2].InnerText.ToLower() == "true" ? radioButtonR1_YES.Checked = true : radioButtonR1_NO.Checked = true;
不太好按照caozhy的方式改造...是不是我这样的思路不对?
如果你无聊非要用,可以这么写
CheckBox ch = cl.ChildNodes[1].ChildNodes[2].InnerText.ToLower() == "true" ? radioButtonR1_YES.: radioButtonR1_NO;
ch.Checked = true;
注意只能跟表达式
,radioButtonR1_YES.Checked = true
是语句
,不是表达式