1

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

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

In .NET, please.

Update: can anyone post a sample scenario of which to use, because both looks very similar to me.

Community
  • 1
  • 1
Adrian Godong
  • 8,802
  • 8
  • 40
  • 62
  • 2
    duplicate http://stackoverflow.com/questions/437026/in-c-what-is-the-difference-between-myint-and-myint – devio Jul 03 '09 at 08:37

4 Answers4

8
  • i++ is a post-increment, meaning this expression returns the original value of i, then increments it.
  • ++i is a pre-increment, meaning this expression increments i, and returns the new value

Many languages aside from C# support this expression behaviour.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
2
int i = 0;
Console.WriteLine(++i); // prints 1
Console.WriteLine(i++); // prints 1 also
Console.WriteLine(i); // prints 2
Amit G
  • 5,165
  • 4
  • 28
  • 29
1

You can Check out this example below..

int a = 0;
int i = 5;

//Value of a: 0, i: 5

a=i++;

//Value of a: 5, i: 6

a=++i;

//Value of a: 7, i: 7
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
0

to make it a bit clearer:

i = 0

print i++  // prints 0 and increases i AFTERWARDS
print i    // prints "1"

i = 0 

print ++i  // increases i FIRST, and then prints it ( "1" )
print i    // prints "1"

As you can see the difference is WHEN the value of the variable is updated, before or after its read and used in the current statement

Tom
  • 3,115
  • 6
  • 33
  • 38