Nested if shorthand.

Concatch

New member
Joined
Sep 3, 2014
Messages
1
Programming Experience
Beginner
Hi,

I'm new to C#. I have some sample code which works. I'm now trying to expand on it.

This code works:
$Name$ == "Consumer" ? "Green" : "Blue"

My understanding is that this is shorthand for "If the variable name is consumer then green, else blue".

I want to make this a nested if statement i.e. If the variable name is x then green, else if variable name is y then purble, else if variable name is z then grey, else if variable name is b then pink etc.

Any help in this shorthand syntax would be great.

Thanks a million
 

tumatauenga

Member
Joined
Aug 30, 2014
Messages
6
Programming Experience
3-5
string color = Name=="X"?"Green":Name=="Y"?"Blue":Name=="Z"?"Grey":Name=="B"?"Pink":"No color specified"

but why would u do this? i mean... really? there are better ways, and more readable for you and anyone else
string result;
string color;
switch(Name)
{
case "X":
result="Green";
break;
case "Y":
result="Blue";
break;
case "Z":
result="Grey";
break;
case "B":
result="Pink";
break;
default
result = "No color specified";
}
color=result;
 
Top Bottom