1

抱歉,很难给这个问题一个合适的标题?

我基本上是在我的网络表单中的几个单选按钮列表中将大量文本附加在一起以形成我的应用程序中的电子邮件正文。所以我的代码如下所示:

StringBuilder body = new StringBuilder();
body.Append("<strong>For question1: </strong>  " + ((rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text : "") + "<br /><br />");

所以我的问题是我是否必须使用以下语法:

((rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text : "")

我真正想要的是:

(rblQuestion1.SelectedIndex > -1) ?  rblQuestion1.SelectedItem.Text 

如果您理解我的观点,我不需要空字符串替代项。

只是我对我的代码有点迂腐。提前致谢。

4

1 回答 1

0

当您本身需要检查时,我看到的唯一选择

rblQuestion1.SelectedIndex > -1

如下:

if (rblQuestion1.SelectedIndex > -1)
  someStringVar = rblQuestion1.SelectedItem.Text;

没有其他解决方案,因为条件语句需要格式化为以下伪代码:

toBeAssigned = boolean expression ? assigner if true : assigner if false;
于 2009-11-25T10:54:23.133 回答