Pre- & Post Increment in C#

前端 未结 6 742
被撕碎了的回忆
被撕碎了的回忆 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:06

    In this example,

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

    you can break it like:

    x = 4++; which is = 5
    x = 4 + ++5; which is 4 + 6
    x = 10
    

    Similarly,

    int x = 4;
    x = x-- - --x;
    

    Here,

    x = 4--; which is = 3
    x = 4 - --3; which is 4 - 2
    x = 2
    

    Simply putting you can say, replace the current value of x, but for every ++ or -- add/subtract a value from x.

提交回复
热议问题