Pre- & Post Increment in C#

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

    The most interesting thing that you'll get a completely different answer with C++.Net compiler.

    int x = 4;
    x = x++ + ++x; // x = 11
    x = 4;
    x = x-- - --x; // x = -1
    

    Of course the difference in results is determined by different semantics - it seems normal. But despite the understanding the fact that two .net compilers don't behave in a similar manner for such basic things confuses me too.

提交回复
热议问题