tags:

views:

185

answers:

7

Hi,

i was totally surprised that the 3rd solution doesn't work (Compiler says: ; is missing).

bool isFoobar = true;

isFoobar == true ? isFoobar = false : isFoobar = true; // [1] works
( isFoobar ? isFoobar = false : isFoobar = true ); // [2] works
isFoobar ? isFoobar = false : isFoobar = true; // [3] failed

Ehm, why does the last one not work?

+1  A: 

under .net 3.5 this work and compile

        bool isFoobar = true;

        var a = isFoobar == true ? isFoobar = false : isFoobar = true; // [1] works
        var b = ( isFoobar ? isFoobar = false : isFoobar = true ); // [2] works
        var c = isFoobar ? isFoobar = false : isFoobar = true; // [3] works

a, b and c are type boolean

Fredou
Yep, none of those 3 lines were valid C# in the original question.
Steve Haigh
+6  A: 

None of those is correct. I get compiler errors with them all.

The correct syntax is:

isFoobar = isFoobar ? false : true;

update

The errors I get with your statements are:

1 & 2:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

3:

Invalid expression term ':'

; expected

; expected

Matt Ellen
you are right! i don't make the assignment to the variable and then all three variants are working. doh!
Viper
would the down voter care to comment? What have I said wrong?
Matt Ellen
+6  A: 

A better solution is:

isFoobar = !isFoobar;
Evan Mulawski
I don't think he meant that he is trying to negate isFooBar he's just using it as a short example of the ternary operator failing unexpectedly.
josh.trow
He wasn't using the ternary statement properly in the first place. I merely gave an alternative solution to his example. If isFoobar was true, make it false, and vice versa.
Evan Mulawski
+1  A: 

You are using it the wrong way. The ternary operator is there to assign a variable based upon a predicate, not to execute code in the two cases.

  var obj = predicate ? true_case : false_case; //if predicate is true, true_case will be assigned to obj.
Femaref
This. The ternary operator is not a universal _if-else shortcut_ as the question title suggests.
Core Xii
A: 

You are mistaken - none of them compile. Try commenting out the third line and see what happens. The true/false part of the ternary statement needs to return a value, which you are not doing.

slugster
+1  A: 

When I try the code, neither of those work. You can't use an expression like that as a statement.

If you use the expressions as expressions, all three work:

bool isFoobar = true;
bool x;

x = isFoobar == true ? isFoobar = false : isFoobar = true;
x = ( isFoobar ? isFoobar = false : isFoobar = true );
x = isFoobar ? isFoobar = false : isFoobar = true;

If you only want to use it as a shortcut for if, and don't want to use the result, you are using it the wrong way. The conditional operator should be used for expressions, not instead of an if statement.

Guffa
A: 

If statement is only like below:

isFoobar ? isFoobar = false : isFoobar = true;

It gives same error as in the question,

If statement is like:

isFoobar ? (isFoobar = false) : (isFoobar = true);

Error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Sheen