What’s the point of post increment ++ operator having higher precedence than preincrement ++ operator? Thus, is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result?
-
I guess it makes sense that result wouldn be different and not wrong – AspOnMyNet Feb 07 '10 at 19:50
-
What would the result of `++x++` be? – Anon. Feb 07 '10 at 20:07
-
4@Anon: The operand of an increment operator must be a property or variable. But the result of such a thing is never a property or variable, its a value. So ++x++ would always be an error in C# regardless of the associativity or precedence; whichever one happened "logically first" would produce something that could not be an operand of the other. – Eric Lippert Feb 07 '10 at 21:13
2 Answers
Let's start with defining some terms, so that we're all talking about the same thing.
The primary operators are postfix "x++" and "x--", the member access operator "x.y", the call operator "f(x)", the array dereference operator "a[x]", and the new, typeof, default, checked, unchecked and delegate operators.
The unary operators are "+x", "-x", "~x", "!x", "++x", "--x" and the cast "(T)x".
The primary operators are by definition of higher precedence than the unary operators.
Your question is
is there a situation where x++ having same level of precedence as ++x would cause an expression to return a wrong result?
It is not at all clear to me what you mean logically by "the wrong result". If we changed the rules of precedence in such a way that the value of an expression changed then the new result would be the right result. The right result is whatever the rules say the right result is. That's how we define "the right result" -- the right result is what you get when you correctly apply the rules.
We try to set up the rules so that they are useful and make it easy to express the meaning you intend to express. Is that what you mean by "the wrong result" ? That is, are you asking if there is a situation where one's intuition about what the right answer is would be incorrect?
I submit to you that if that is the case, then this is not a helpful angle to pursue because almost no one's intuition about the "correct" operation of the increment operators actually matches the current specification, much less some hypothetical counterfactual specification. In almost every C# book I have edited, the author has in some subtle or gross way mis-stated the meaning of the increment operators.
These are side-effecting operations with unusual semantics, and they come out of a language - C - with deliberately vague operational semantics. We have tried hard in the definition of C# to make the increment and decrement operators sensible and strictly defined, but it is impossible to come up with something that makes intuitive sense to everyone, since everyone has a different experience with the operators in C and C++.
Perhaps it would be helpful to approach the problem from a different angle. Your question presupposes a counterfactual world in which postfix and prefix ++ are specified to have the same precedence, and then asks for a criticism of that design choice in that counterfactual world. But there are many different ways that could happen. We could make them have the same precedence by putting both into the "primary" category. Or we could make them have the same precedence by putting them both into the "unary" category. Or we could invent a new level of precedence between primary and unary. Or below unary. Or above primary. We could also change the associativity of the operators, not just their precedence.
Perhaps you could clarify the question as to which counterfactual world you'd like to have criticized. Given any of those counterfactuals, I can give you a criticism of how that choice would lead to code that was unnecessarily verbose or confusing, but without a clear concept of the counterfactual design you're asking for criticism on, I worry that I'd spend a lot of time criticising something other than what you actually have in mind.
Make a specific proposed design change, and we'll see what its consequences are.

- 647,829
- 179
- 1,238
- 2,067
-
I thought I knew what I've meant with my question, but now I see my question didn't make much sense. Anyways, thank you for your help – AspOnMyNet Feb 08 '10 at 19:40
-
@AspOnMyNet: I think there's a good question in here somewhere, it's just not clear what precisely the question is. If you want to try and clarify it, I'm happy to take a crack at it. – Eric Lippert Feb 08 '10 at 20:32
-
I didn't have a clear concept in my mind of what exactly it was bothering me,it was more of a vague idea, which as it turned out wasn't much of an idea at all :D. I too sense there is a question in there somewhere, but I feel I should be much more math savvy in order to come up with any meaningfull questions on the subject – AspOnMyNet Feb 09 '10 at 18:52
John, you have answered the question yourself: these two constructions are mostly used for function calls: ++x
- when you want first to increase the value and then call a function, and x++
when you want to call a function, and then make an increase. That might be very useful, depending on the context. Looking at return x++
vs return ++x
I see no point for error: the code means exactly how it reads :) The only problem is the programmer, who might use these two constructions without understanding the operator's precedence, and thus missing the meaning.

- 10,431
- 16
- 76
- 128