In C# what is the difference between myInt++ and ++myInt?

后端 未结 11 1818
我寻月下人不归
我寻月下人不归 2020-12-06 03:11

I\'m having a hard time understanding what the difference is between incrementing a variable in C# this way:

myInt++;

and

         


        
11条回答
  •  北海茫月
    2020-12-06 03:43

    I think it is simplest to understand in terms of the order of evaluation and modification.

    • The postfix operator (x++) evaluates first and modifies last.
    • The prefix operator (++x) modifies first and evaluates last.

    (the order of operations is evident in the typed order of the operator and the variable)

    I.e. when used in an expression, the postfix operator evaluates and uses the variable's current value in the expression (a return statement or method call) before applying the operator. With the prefix operator it is the other way around.

提交回复
热议问题