Why can't I use break in a C# ternary expression?

后端 未结 2 1948
终归单人心
终归单人心 2021-01-29 07:24

I am trying to convert the if else clause to a ternary within a while loop, however it\'s not allowing me to have a break after the question mark, pointing an error at the break

2条回答
  •  庸人自扰
    2021-01-29 07:25

    Because the ternary is not a shorter way to write an if-else structure, it's a short way to write an expression that picks one of two values based on some condition. break is a flow-control statement, not a value.

    If it helps, think of:

    someVar = cond ? a : b;
    

    as of:

    someVar = getValue(cond);
    

提交回复
热议问题