C# – shorthand syntax for a radiobuttonlist SelectedItem.Text only if one is selected

asp.netc++radiobuttonlist

Sorry, difficult to give the question a proper title?

I'm basically appending a lot of text together to form the email body in my application from several radiobuttonlists in my web form. So my code looks like this:

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

So my question is do I have to use the following syntax:

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

Where what I really want is:

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

I don't need the empty string alternative if false, if you understand my point.

It's just me being a bit pedantic with my code really. Thanks in advance.

Best Solution

The only alternative I see when you per se need to check for

rblQuestion1.SelectedIndex > -1

is the following:

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

There is no other solution because conditional statements need to be formatted in the following pseudo-code:

toBeAssigned = boolean expression ? assigner if true : assigner if false;
Related Question