Are you looking for a conditional operator or a three-dimensional operator ?: ::
His form
condition ? value_if_true : value_if_false
For instance:
Console.Write((!Example) ? "Example is false" : "Example is true");
Or my personal preferences,
Console.Write(Example ? "Example is true" : "Example is false");
so that I never think what happens when "not Example is false".
Note that you cannot put arbitrary code for value_if_true and value_if_false - it must be an expression, not an expression. So the above is true because
(!Example) ? "Example is false" : "Example is true"
is string , you can write:
string message = (!Example) ? "Example is false" : "Example is true"; Console.Write(message);
However you cannot do
(!Example) ? Console.Write("Example is false") : Console.Write("Example is true")
for example, since Console.Write(..) does not return a value, or
(!Example) ? { a = 1; "Example is false" } : "Example is true"
because { a = 1; "Example is false" } { a = 1; "Example is false" } not an expression.
source share