Chain Increment Operators

Ashley Medway

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.

Jon Skeet

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Logical Operators and increment operators

From Dev

/windowApache Flink chain operators

From Dev

RxJava operators or Kotlin chain

From Dev

Increment and logical operators precedence

From Dev

Increment operators and "undefined behaviour"

From Dev

Increment and logical operators precedence

From Dev

Chain arithmetic operators in dplyr with %>% pipe

From Java

Behaviour of increment and decrement operators in Python

From Dev

increment and addition operators precedence, cpp

From Dev

Pre/Post increment operators and arrays

From Dev

Using increment operators with some functions

From Dev

Is it safe to put increment/decrement operators inside ternary/conditional operators?

From Dev

Are the decrement and increment shorthand operators are types of shorthand assignment operators?

From Dev

Dynamically chain Specification using where & and operators

From Dev

Cases where to use Pre-Increment or Post-Increment Operators

From Dev

Pre-increment and post-increment operators in Swift

From Dev

Is increment/decrement operators Undefined Behaviour in Bash?

From Dev

Multiple increment operators on the same line Python

From Dev

Increment and Decrement operators in scheme programming language

From Dev

Pre and post increment operators in different classes

From Dev

C operator precendence: Increment and logical operators

From Dev

Java expression interpretation rules of decrement/increment operators

From Dev

Increment and Decrement operators in scheme programming language

From Dev

C operator precendence: Increment and logical operators

From Dev

Confusion using Increment and decrement operators in Java

From Dev

Is there a way in RxJava/RxAndroid to let the chain of operators finish even after unsubscribing?

From Dev

Any easy way to chain two comparison <=> operators in Ruby?

From Dev

RxJava: How to conditionally apply Operators to an Observable without breaking the chain

From Dev

Pre-Increment Operators when Using the Variable on the Same Line

Related Related

  1. 1

    Logical Operators and increment operators

  2. 2

    /windowApache Flink chain operators

  3. 3

    RxJava operators or Kotlin chain

  4. 4

    Increment and logical operators precedence

  5. 5

    Increment operators and "undefined behaviour"

  6. 6

    Increment and logical operators precedence

  7. 7

    Chain arithmetic operators in dplyr with %>% pipe

  8. 8

    Behaviour of increment and decrement operators in Python

  9. 9

    increment and addition operators precedence, cpp

  10. 10

    Pre/Post increment operators and arrays

  11. 11

    Using increment operators with some functions

  12. 12

    Is it safe to put increment/decrement operators inside ternary/conditional operators?

  13. 13

    Are the decrement and increment shorthand operators are types of shorthand assignment operators?

  14. 14

    Dynamically chain Specification using where & and operators

  15. 15

    Cases where to use Pre-Increment or Post-Increment Operators

  16. 16

    Pre-increment and post-increment operators in Swift

  17. 17

    Is increment/decrement operators Undefined Behaviour in Bash?

  18. 18

    Multiple increment operators on the same line Python

  19. 19

    Increment and Decrement operators in scheme programming language

  20. 20

    Pre and post increment operators in different classes

  21. 21

    C operator precendence: Increment and logical operators

  22. 22

    Java expression interpretation rules of decrement/increment operators

  23. 23

    Increment and Decrement operators in scheme programming language

  24. 24

    C operator precendence: Increment and logical operators

  25. 25

    Confusion using Increment and decrement operators in Java

  26. 26

    Is there a way in RxJava/RxAndroid to let the chain of operators finish even after unsubscribing?

  27. 27

    Any easy way to chain two comparison <=> operators in Ruby?

  28. 28

    RxJava: How to conditionally apply Operators to an Observable without breaking the chain

  29. 29

    Pre-Increment Operators when Using the Variable on the Same Line

HotTag

Archive