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

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

    ++myInt will add one to myInt before executing the line in question, myInt++ will do it afterward.

    Examples:

    int myInt;
    
    myInt = 1;
    someFunction(++myInt); // Sends 2 as the parameter
    
    myInt = 1;
    someFunction(myInt++); // Sends 1 as the parameter
    

提交回复
热议问题