Optimize conditional operators branching in C#

Posted by abatishchev on Stack Overflow See other posts from Stack Overflow or by abatishchev
Published on 2010-04-27T18:50:15Z Indexed on 2010/04/27 18:53 UTC
Read the original article Hit count: 275

Hello. I have next code:

return this.AllowChooseAny.Value ?
       radioSpecific.Checked ?
          UserManager.CurrentUser.IsClient ? txtSubject.Text : subjectDropDownList.SelectedItem.Text :
          String.Empty :
       UserManager.CurrentUser.IsClient ? txtSubject.Text : subjectDropDownList.SelectedItem.Text;

or in less complex form:

return any ?
    specified ?
       isClient ? textbox : dropdown :
       empty :
    isClient ? textbox : dropdown;

or in schematic form:

                     |
                    any
              /            \
      specified             isClient
      /        \           /        \
  isClient    empty     textbox  dropdown
  /       \
textbox  dropdown

Evidently I have a duplicated block on two different levels. Is it possible to optimize this code to probably split them to one? Or something like that..

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#