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

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

    If for example you do something like this:

    int myInt = 5;
    
    Foo(++myInt);
    
    void Foo(int x)
    {
       Console.WriteLine(x);
    }
    

    This will print out 6.

    Foo(myInt++);
    

    will print out 5;

    Basically, ++myInt increments FIRST uses the variable SECOND. myInt++ is the opposite.

提交回复
热议问题