Why can you not chain operators?
int test = 5;
test++++;
OR
int test = 5;
++test++;
This code gives a compile time error.
The operand of an increment or decrement operator must be a variable, property or indexer.
I fully understand this, if allowed, would be a complete code smell and has almost no real world usage. I don't fully understand why this results in an error. I would almost expect the value of test
to be 7 after each statement.
Basically, it's due to section 7.6.9 of the specification:
The operand of a postfix increment or decrement operation must be an expression classified as a variable, a property access, or an indexer access. The result of the operation is a value of the same type as the operand.
The second sentence means that i++
is classified as a value (not a variable, unlike i
) and the first sentence stops that from being the operand of the operator.
I suspect it was designed that way for simplicity, to avoid weird situations you really don't want to get into - not just the code you've given, but things like:
Foo(ref i++);
i++ = 10;
See more on this question at Stackoverflow