How to do a quick inline method with conditional?

191 Views Asked by At

This is what I'd like to do:

string x = if(true) "String Val" else "No String Val";

Is that possible?

2

There are 2 best solutions below

0
Justin Niessner On BEST ANSWER

What you're talking about is called a conditional statement:

string x = boolVal ? "String Val" : "No String Val";

If you really want the string to have no value if the bool is false, you could change to:

string x = boolVal ? "String Val" : null;
1
Luke Baulch On
string x = condition ? trueValue : falseValue;

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx