0

This is what I'd like to do:

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

Is that possible?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user259286
  • 977
  • 3
  • 18
  • 38
  • Probably not a valid answer to this question (hence a comment instead): if your condition is a null check, e.g. `string x = (s != null) ? s : "Something else"`, you can do `string x = s ?? "Something else"` – Flynn1179 Feb 11 '11 at 00:17

2 Answers2

1

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;
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0
string x = condition ? trueValue : falseValue;

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

Luke Baulch
  • 3,626
  • 6
  • 36
  • 44
  • Just to clarify: those values can actually be any expression. They must both be strings though, or in the general case, they must be of the same type. – Flynn1179 Feb 11 '11 at 00:31