Pre- & Post Increment in C#

前端 未结 6 739
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 05:46

I am a little confused about how the C# compiler handles pre- and post increments and decrements.

When I code the following:

int x = 4;
x = x++ + ++x         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 06:01

    I think the explanation for the ++ + ++ case is wrong:

    command...........value of x

    ..................undefined

    int x=4 ..........4

    x++...............5 (first summand is 4)

    ++x...............6 (second summand is 6)

    x=summand1+summand2 ..4+6=10

    Analogous the explanation for the -- - -- case is

    command...........value of x

    ..................undefined

    int x=4 ..........4

    x--...............3 (subtactor is 4)

    --x...............2 (subtrahend is 2)

    x=subtractor-subtrahend ..4-2=10

提交回复
热议问题