简体   繁体   English

C#条件语句

[英]C# conditional statement

I want to switch the value for isFollowing. 我想切换isFollowing的值。 If it's true I want isFollowing = false and the opposite too. 如果是真的,我要isFollowing = false,反之亦然。

Instead of 'if' statement i am using ? 我使用的不是'if'语句? :

        isFollowing == true ? isFollowing = false : isFollowing = true;

But this isn't working. 但这是行不通的。 it gives "Only assignment, call, increment, decrement, and new object expressions can be used as a statement" Why is that ? 它给出了“仅赋值,调用,递增,递减和新的对象表达式可以用作语句”的原因?

Thanks in advance 提前致谢

If you wanted to keep it as a ternary, you could do 如果您想将其保留为三元组,则可以

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

However, this is a much shorter equivalent: 但是,这是一个简短得多的等效项:

isFollowing = !isFollowing;

The reason why your code didn't work is that a ternary on its own is an expression, and not all expressions are valid statements. 您的代码无法正常工作的原因是三元本身就是一个表达式,而并非所有的表达式都是有效的语句。 By converting it to an assignment to the value of a ternary, it becomes a statement. 通过将其转换为三进制值的赋值,它成为一条语句。

Your code would be most likely valid in C, as long as it satisfied operator precedence rules, but C# will not allow you to do this. 只要满足操作符优先级规则,您的代码就很可能在C语言中有效,但是C#不允许您这样做。 As a practice, it's best to limit your ternary branches to not having side effects anyway, even if the language allows you to do it. 作为一种实践,即使语言允许,也最好将三元分支限制为无副作用。

Use: 采用:

isFollowing = !isFollowing;

To make this work using the ternary operator: 要使用三元运算符进行这项工作:

isFollowing = isFollowing ? false : true;

But don't do that, just use the first version. 但是不要那样做,只需使用第一个版本即可。

The reason why what you have doesn't work is because the ternary operator is of the form 您拥有的东西不起作用的原因是因为三元运算符具有以下形式

conditon ? expression_if_condition_is_true : expression_if_condition_is_false;

and this evaluates to an expression ( expression_if_condition_is_true if condition is true and expression_if_condition_is_false if condition is false ). 和(这个计算结果为表达expression_if_condition_is_true如果conditiontrueexpression_if_condition_is_false如果conditionfalse )。 Expressions can not be used as statements unless they are method calls, increments (ie, i++ ), decrements, allocations using new and assignments. 表达式不能用作语句,除非它们是方法调用,递增(即i++ ),递减,使用new和赋值的分配。

Surely an easier way is: 当然,更简单的方法是:

isFollowing = !isFollowing;

Otherwise using ?: 否则使用?:

isFollowing = isFollowing ? false : true;

The reason you're getting the error is that you have an assignment in the middle of the expresion: 出现错误的原因是在表达式中间有一个赋值:

isFollowing == true ? isFollowing = false : isFollowing = true;
                                  ^
                                here

The ternary operator is an expression, and you cannot have this type of expression by itself, you need to assign it to something. 三元运算符是一个表达式,您不能单独拥有这种类型的表达式,需要将其分配给某些对象。

The other answers here give you the solution on how to rewrite it: 这里的其他答案为您提供有关如何重写它的解决方案:

isFollowing = !isFollowing;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM