Search code examples
c#post-incrementpre-increment

Different between x++ and ++x


I've known that primary operator (x++) is different form unary operator (++x) when combine with another operator in a statement.

But I wonder whether those two operator is same when leave them alone on the statement. I mean about compiled code, time to run, ... between:

++x;

and

x++;

Which x is an integer variable.


Solution

  • It is the same in disassembly (not IL) on Windows 8 x64:

                    ++x;
    0000004a  inc         dword ptr [ebp-40h] 
                    x++;
    0000004d  inc         dword ptr [ebp-40h] 
    

    As you can see, both statements are an inc instruction. I used this code to try out:

    int x = 0;
    for (int i = 0; i < 10; i++)
    {
        ++x;
        x++;
    }