Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no difference when written on its own (as shown) - in both cases myInt will be incremented by 1.</p> <p>But there is a difference when you use it in an expression, e.g. something like this:</p> <pre><code>MyFunction(++myInt); MyFunction(myInt++); </code></pre> <p>In the first case, myInt is incremented and the new/incremented value is passed to MyFunction(). In the second case, the old value of myInt is passed to MyFunction() (but myInt is still incremented before the function is called).</p> <p>Another example is this:</p> <pre><code>int myInt = 1; int a = ++myInt; // myInt is incremented by one and then assigned to a. // Both myInt and a are now 2. int b = myInt++; // myInt is assigned to b and then incremented by one. // b is now 2, myInt is now 3 </code></pre> <p>BTW: as <a href="https://stackoverflow.com/users/52682/don-music">Don</a> pointed out in a comment the same rules are also valid for decrement operations, and the correct terminology for these operations are:</p> <pre><code>++i; // pre-increment i++; // post-increment --i; // pre-decrement i--; // post-decrement </code></pre> <p>As <a href="https://stackoverflow.com/users/22656/jon-skeet">Jon Skeet</a> points out:</p> <blockquote> <p>Others have shown where it makes a difference, and have commented that as a single statement it doesn't make a difference.</p> <p>I'd like to add that it's almost always a bad idea to use it where it makes a difference. I suspect there may be some times where it's more readable to have code such as:</p> <pre><code>Console.WriteLine("Foo: {0}", foo++); </code></pre> <p>than:</p> <pre><code>Console.WriteLine("Foo: {0}", foo); foo++; </code></pre> <p>... but they're very rare! The latter of these two samples makes the ordering crystal clear immediately - the former requires a bit of thinking (to my poor brain, anyway). Think of the readability first.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload