+ 3

Is there any method to simplify if else statement if the statement more than 2??

string name = ""; if(RadioButton1.Checked) { name = Orange; } else if(RadioButton2.Checked) { name = Apple; else { name = Peer; } Is there any method to simplify this code?

30th Aug 2019, 9:54 AM
hideyori
hideyori - avatar
5 Respostas
+ 6
If what you meant by "simplify" was less lines then you can try ternary operator, but IMHO that code stands better in readability level. Ternary operator: <condition> ? <true-block> : <false-block> Equivalent to: If <condition> evaluates to true execute <true-block> else execute <false-block> You can nest this, but readability is rather poor. Choice is yours : )
30th Aug 2019, 10:08 AM
Ipang
+ 1
thank you
30th Aug 2019, 10:20 AM
hideyori
hideyori - avatar
+ 1
I can suggest one more: If(var)do this; It's most simlify without "else" statsment. Or If(var)do this; Else do this; https://code.sololearn.com/c9pN2WesEEZI/?ref=app
30th Aug 2019, 4:26 PM
id001x
id001x - avatar
0
You can get the name with event handler listener
31st Aug 2019, 2:42 PM
Toan Phan