Take the 2-minute tour ×

I can't seem to find what I need on google, and bet I'll get quick answer here.

    String str;
    bool b = true;
    b ? str="true" : str="false";


that ? : syntax looks correct to me. I'm getting compiler error though.

error CS1002: ; expected
error CS1525: Invalid expression term ':'
error CS1002: ; expected

Not sure about the csharp syntax, but that builds in cpp. Please help! thanks!

UPDATE: About 10 of you give the correct answer LOL, so I'll just award to the first person who submitted it.

interesting Syntax, and I think I actually like it better than c++ syntax.

The actual code I was doing this for is:

ftp.ConnectMode = job.FTPUsePassiveMode ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE;
share|improve this question
Others have already given the proper solution; it is possible that order of precedence is the problem. b ? (str="true") : (str="false"); Even if that works, I wouldn't use it like that. –  Matthew Mar 2 '10 at 19:14
@cchampion: "interesting Syntax, and I think I actually like it better than c++ syntax." -- The c++ syntax is the same. It should be str = b ? "true" : "false" in both languages. –  Josh Mar 2 '10 at 19:59

7 Answers 7

up vote 15 down vote accepted

Your code should read:

str = b ? "true" : "false";

However, this is akin to just calling b.ToString().ToLower(). That said, I suspect your actual use-case is a little more complex than just converting the Boolean value to a string.

As indicated in the comments, the conditional operator returns a value; it is not for control flow like if/else.

share|improve this answer
The relevant point is that the ternary operator (?:) is an expression that returns a value, not a control flow mechanism (like if/else). –  Tyler McHenry Mar 2 '10 at 19:13
@Tyler: Well said. –  Josh Mar 2 '10 at 19:17
str = b ? "true" : "false";

But you could just do this:

str = b.ToString();

Or even cut out the middleman altogether:

share|improve this answer
Strictly speaking, you'd get "True" and "False" from ToString() (and thus WriteLine), not "true" and "false", but I'm not sure it would matter to me. You could always do ToString().ToLower(). –  tvanfosson Mar 2 '10 at 19:17
str = (b) ? "true" : "false";
share|improve this answer
But... Why enclose b in parentheses ? I fail to see the ambiguity it resolves. –  Raphaël Saint-Pierre Mar 2 '10 at 19:33

The ternary operator doesn't allow for statement switching, only value switching. You want to do this:

str= b ? "true" : "false"

(obviously b.ToString()) is a better solution for this particular problem, but I'm assuming this is just an example).

share|improve this answer
expression selection... and str="true" IS an expression. Try a = b? (str="true"): (str="false"); and I think you'll find that it works just fine. –  Ben Voigt Mar 2 '10 at 19:14
And to address the point that this is valid c++ code, that's because you can use statements to get values in c++. So the following would be valid in c++ (although very confusing): a = b ? c=d : c=f –  Josh Mar 2 '10 at 19:15
@Ben: Yes, expression selection is a better term, and an assignment is an expression (returning the value placed into the lvalue), but the output of the ternary operator can't be ignored. –  Adam Robinson Mar 2 '10 at 19:22

What everyone else said, and: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

share|improve this answer
and en.wikipedia.org/wiki/Ternary_operation –  jschmier Mar 2 '10 at 19:17

The ternary operator can't be the top-level of a statement in C#, because C# requires that top-level expressions have a side-effect.

share|improve this answer

Just out of curiosity, why not just do this:

bool b = true;
string str = b.ToString();

In .NET, value types automatically convert their value to a string when .ToString() is called...including booleans.

share|improve this answer
while that's a good suggestion it doesn't answer the question of why the ternary operator isn't working in this case. –  Josh Mar 2 '10 at 19:16
The point was to eliminate the problem completely. Other answers adequately explained why it wasn't working (because he was using it incorrectly.) I don't like to repeat answers, especially when there are a bunch of the same. I don't think this deserved a down vote, as it was a valid solution to the problem, even if it didn't explicitly answer his question. –  jrista Mar 2 '10 at 20:58

Your Answer


