I played using lvalue as a result of a conditional statement ( cond ? expr1 : expr2 ).
Consider the following example.
class Value { public int MagicNumber { get; private set; } = 0; public void Increment() { } } void Main() { Value v1, v2; bool condition = ; (condition ? v1 : v2).Increment();
Now I would suggest that based on the value of condition , either v1 or v2 is incremented. This is true if Value ( (1) ) is a class. As soon as I change it to struct , it becomes a value type, and line (3) does nothing (I suspect that either v1 or v2 copied, incremented and discarded). Before that it makes sense. Strange behavior begins after (2) ( condition ) is known at compile time (for example, by defining its const bool ). Then some optimization appears, and one of v1 or v2 actually increases in place.
My question is: what should be the correct behavior of the conditional operator in the following case
(condition ? v1 : v2).Increment();
once v1 and v2 is equal to struct . Should it depend on the condition compile-time constant?
source share