Ternary operator in PowerShell

后端 未结 13 2104
忘掉有多难
忘掉有多难 2020-11-28 03:56

From what I know, PowerShell doesn\'t seem to have a built-in expression for the so-called ternary operator.

For example, in the C language, which supports the terna

13条回答
  •  佛祖请我去吃肉
    2020-11-28 04:31

    Since a ternary operator is usually used when assigning value, it should return a value. This is the way that can work:

    $var=@("value if false","value if true")[[byte](condition)]
    

    Stupid, but working. Also this construction can be used to quickly turn an int into another value, just add array elements and specify an expression that returns 0-based non-negative values.

提交回复
热议问题