0

I've always written for loops in C# using for (int i = 0; i < 10; i++).

I've been reading up on the best-practices for JavaScript (JavaScript: The Good Parts), and one of them is to prefer x += 1 over x++.

Are there any differences, in C#, between the two in areas such as performance, atomicity, and functionality.

I ask, because there are functional differences between ++x and x++ in C# (and C++ and probably most other C based languages); the former being pre-increment where it increments the variable and then returns the value, and the latter being post-increment where it returns the value and then increments it (actually, these two subtle differences are what's piqued my interest in adopting the x += 1 strategy in my C# code)

Update:

Here's two methods, method1 uses ++x and method2 uses x += 1:

   static void method1()
    {
        int x = 1;
        int y = ++x;

        Console.WriteLine(x);
        Console.WriteLine(y);
    }

    static void method2()
    {
        int x = 1;
        int y = x += 1;

        Console.WriteLine(x);
        Console.WriteLine(y);
    }

This produces the following IL:

Method 1:

enter image description here

Method 2:

enter image description here

There appears to be some minor differences, but my understanding of IL isn't enough to answer the question 'are there any differences'.

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
  • You can answer this question yourself: Look at the IL. – Daniel Mann Nov 15 '14 at 19:42
  • 1
    It is just a syntactical construct in C#, the MSIL that the compiler generates is identical. – Hans Passant Nov 15 '14 at 19:43
  • In C#, you shouldn't think of the difference between pre- and post-increment as when the side effect happens. The side effect occurs when the operator is evaluated in both cases. [The difference is which value is returned the one before incrementing or after.](http://stackoverflow.com/a/3346729/517852) – Mike Zboray Nov 15 '14 at 20:13
  • When used in the context you describe (a loop control variable), there is no difference between `++i` and `i++`. http://stackoverflow.com/questions/467322/is-there-any-performance-difference-between-i-and-i-in-c/467944#467944. Testing with `x += 1` shows that it, too, generates the same code. – Jim Mischel Nov 15 '14 at 20:17

1 Answers1

3

No difference at all. The Intermediate Language translated from x += 1 and x++ is identical:

int x = 0;
x++; // or x += 1;

The IL code is:

IL_0001:  ldc.i4.0    
IL_0002:  stloc.0     // x
IL_0003:  ldloc.0     // x
IL_0004:  ldc.i4.1    
IL_0005:  add         
IL_0006:  stloc.0     // x
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • Hi Jason - thanks for your answer. I updated the question with an example and some resulting IL. There does seem to be minor differences. – Steve Dunn Nov 15 '14 at 20:18
  • 1
    @SteveDunn: the IL isn't really all that relevant. I mean, if the IL were the same, you could end there. But even if it's different, very minor differences are likely to be erased during optimized JIT compilation. The bottom line here is that in terms of incrementing an integer variable, there is zero technical reason to prefer one syntax over another. – Peter Duniho Nov 15 '14 at 22:44