During experiments with triple and zero coalescing operators in C #, I found that you can use the assignments on the right side of the expressions, for example, this is a valid C # code:
int? a = null; int? b = null; int? c = a ?? (b = 12); int? d = a == 12 ? a : (b = 15);
Oddly enough, not only the assignment on the right side of the expression is evaluated on its own right side (this means that the third line here is evaluated as 12
, and not something like b = 12 => void
), but this assignment also works effectively. so two variables are assigned in one expression. You can also use any calculated expression on the right side of this assignment with any variable available.
This behavior seems very strange to me. I remember that I had problems with if (a = 2)
instead of comparing if (a == 2)
in C ++, which is always evaluated as true
, and this is a common mistake after switching from Basic / Haskell to C ++.
Is this a documented feature? Is there a name for this?
source share