Reputation: 469
So I have a small snippet of code that I may use when I want to quickly check an integer before deciding what a string value will be:
string status = (statusID == 0 ? "Inactive" : "Active");
However, I cannot remember what this practice / piece of code is called of referred to as. I wanted to implement a similar bit of code, but with two parameter checks, to check for two different numbers, giving three possible outcomes. Is this possible? Or would it be more suitable to expand this out into two usages of this code, checking for a certain string, or expand the functionality into a method?
Upvotes: 2
Views: 85
Reputation: 125620
?:
is conditional operator in c#: ?:
Operator (C# Reference)
Just put another ?:
statement in else part of the first one:
string status = (statusID == 0 ? "Inactive" : (statusID == 1 ? "Active" : "OtherOne"));
That's gonna return "Inactive" for statusID == 0
, "Active" for statusID == 1
and "OtherOne" for others.
Upvotes: 4