Felix Weir
Felix Weir

Reputation: 469

Checking an integer then returning a string

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

Answers (1)

MarcinJuraszek
MarcinJuraszek

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

Related Questions