Using the ternary operator for multiple operations

后端 未结 5 1839
萌比男神i
萌比男神i 2020-12-30 02:44

How can I use the ternary ? : condition to perform multiple operations, if expression is true/false?

wbsource = (exp) ? (Do one thing) : (Do secon

5条回答
  •  星月不相逢
    2020-12-30 03:26

    The conditional operator, which is a ternary operator (not a unary operator), is not a replacement for an if statement. It is an operator that returns one of two results. While you can chain this to some extent:

    var result = someBool ? "a" : (otherBool ? "b" : "c");
    

    That gets a little hard to read. Further, you're trying to call the Save() function, which does not return a result, hence you cannot use it with this operator.

提交回复
热议问题