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

后端 未结 11 1840
我寻月下人不归
我寻月下人不归 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:34

    It doesn't unless it is in an expression, i.e.

    myInt=1;
    x=myInt++;
    

    is different to:

    myInt=1;
    x=++myInt;
    

    The first assigns 1 to x, because the assignment happens before the increment.

    The second assigns 2 to x, because the assignment happens after the increment.

提交回复
热议问题