ternary conditional operator with statement support #7289
-
in C#, the ternary conditional operator, AKA the ?: operator, is used to assign values based on a conditional. You can symbolize its intent as follows: variable = conditional ? yes expression : no expression What I would love for this syntax to offer, is statement support, in which I don't have to declare a variable and assign it the value of the ternary expression. Goes something like this: conditional ? Foo() : Bar(); Where Foo() and Bar() can be void methods.
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
You're comparing the ternary to the longest form of the You could just use if (condition) Foo(); else Bar(); and then that isn't much longer, and it's a lot easier to read... |
Beta Was this translation helpful? Give feedback.
-
There was talk around four years ago of allowing switch statements to use the switch expression conditional switch {
true => Foo(),
false => Bar()
}; Since there seems little chance of even this style of "void in expressions" happening, I think the chances of the team embracing statements in the ternary condition operator are pretty much non-existent. |
Beta Was this translation helpful? Give feedback.
-
Duplicate of #6013. |
Beta Was this translation helpful? Give feedback.
-
Excellent news. I look forward to reading the results from that working group. |
Beta Was this translation helpful? Give feedback.
Duplicate of #6013.