Using the ternary operator for multiple operations

后端 未结 5 1826
萌比男神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

    Why can't I perform three operations between ? and :

    Because these are operands, which are expressions. Each expression evaluates a value; you want multiple statements. From Eric Lippert's blog post about foreach vs ForEach:

    The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects.

    The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect. The call site of this thing would look an awful lot like an expression (though, admittedly, since the method is void-returning, the expression could only be used in a “statement expression” context.)

    You should absolutely write this using an if block. It's clearer.

    If you really, really want to use the conditional operator for this, you could write:

    // Please, please don't use this.
    Func<string> x = () => {
        Properties.Settings.Default.filename = fp;
        Properties.Settings.Default.Save();
        return fp;
    };
    
    string filename = fp == null ? Properties.Settings.Default.file : x();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 03:32

    If you really, really want to, you could use a function which has side effects:

    filename = (fp!=null) ? DoOneThing(...) : DoAnotherThing(...);
    

    Though whoever maintains your code won't thank you.

    0 讨论(0)
  • 2020-12-30 03:37

    If this was c you'd be OK thanks to the "comma operator":

    int b;
    int a = (1==1) ? (b=6, somemethod(), 1) : (b=7, 2);
    

    Here b will be set to 6, somemethod will be called and then a is set to 1.

    Thankfully that was one feature that was not ported over, use if..else it's much clearer.

    0 讨论(0)
  • 2020-12-30 03:42

    Short answer, use an if block, its the only sane thing to do.

    Other answer, for the dirty, smelly insane individual.

    filename = (fp!=null) ? Func<string> {fp = Properties.Settings.Default.filename; Properties.Settings.Default.Save; return fp;} : Properties.Settings.Default.file; 
    
    0 讨论(0)
提交回复
热议问题