if-else快捷方式惊喜

Vip*_*per 0 c# syntax

我很惊讶第三种解决方案不起作用(Compiler说:) ; 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
Run Code Online (Sandbox Code Playgroud)

嗯,为什么最后一个不起作用?

Mat*_*len 7

这些都不正确.我发现编译器错误.

正确的语法是:

isFoobar = isFoobar ? false : true;
Run Code Online (Sandbox Code Playgroud)

更新

我在你的陈述中得到的错误是:

1和2:

只有赋值,调用,递增,递减和新对象表达式才能用作语句

3:

无效的表达式术语':'

; 预期

; 预期


Eva*_*ski 7

更好的解决方案是:

isFoobar = !isFoobar;
Run Code Online (Sandbox Code Playgroud)