none
Is there difference between ++i and i++ for C# RRS feed

  • Question

  • I read at C++ FAQ that ++i is better than i++ for C++.
    Is there difference between ++i and i++ for C# ?
    Monday, April 28, 2008 11:48 AM

Answers

  • ++i is very different then i++.

     

    ++i means that when your code is executing it will first do i = i + 1 and then read it.

    i++ means that when your code is executing it will first read it and do the i = i + 1 after it has been read.

     

    So when u call a function this way:

     

    public void Test()

    {

    int i = 1;

    ShowValue(++i); // <-- will output 2

     

    i = 1; // <-- reset i

    ShowValue(i++); // <-- will output 1

     

    }

     

    U can't say ++i is better then i++ because they are 2 very different things. Unless u can tell me why Smile

     

    Monday, April 28, 2008 12:01 PM
  • I assume the OP is talking about a performance difference, not a correctness difference.

    The compiler indeed optimizes away any difference between ++i and i++ if you don't use the return value.  So the stand-alone ++i or i++ gets compiled to the same code.

    Now consider non-primitives when the return value is used.  If the type is a class (reference type), then no copy of it is made anyway in the operator++ implementation.  If the type is a struct (value type), then a copy of it is made anyway if you assign the result of operator++ to anything.  Therefore, there is no performance difference.
    Monday, April 28, 2008 4:09 PM
  • As already mentionned, there is a significant difference...

    If i remember well, the compiler is smart enough to optimize a i++ to ++i if you don't use the i in the statement...

    Monday, April 28, 2008 12:10 PM

All replies

  • ++i is very different then i++.

     

    ++i means that when your code is executing it will first do i = i + 1 and then read it.

    i++ means that when your code is executing it will first read it and do the i = i + 1 after it has been read.

     

    So when u call a function this way:

     

    public void Test()

    {

    int i = 1;

    ShowValue(++i); // <-- will output 2

     

    i = 1; // <-- reset i

    ShowValue(i++); // <-- will output 1

     

    }

     

    U can't say ++i is better then i++ because they are 2 very different things. Unless u can tell me why Smile

     

    Monday, April 28, 2008 12:01 PM
  • As already mentionned, there is a significant difference...

    If i remember well, the compiler is smart enough to optimize a i++ to ++i if you don't use the i in the statement...

    Monday, April 28, 2008 12:10 PM
  • I assume the OP is talking about a performance difference, not a correctness difference.

    The compiler indeed optimizes away any difference between ++i and i++ if you don't use the return value.  So the stand-alone ++i or i++ gets compiled to the same code.

    Now consider non-primitives when the return value is used.  If the type is a class (reference type), then no copy of it is made anyway in the operator++ implementation.  If the type is a struct (value type), then a copy of it is made anyway if you assign the result of operator++ to anything.  Therefore, there is no performance difference.
    Monday, April 28, 2008 4:09 PM
  • i++ means 'give the value of i, then increment'

    ++i means 'increment i, then give the value'



    • Edited by Jaison Joe Monday, February 9, 2015 12:37 PM
    Monday, February 9, 2015 12:33 PM