Click here to Skip to main content
15,887,386 members
Home / Discussions / C#
   

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 1:32
N8tiv28-Apr-13 1:32 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
NotPolitcallyCorrect28-Apr-13 1:39
NotPolitcallyCorrect28-Apr-13 1:39 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 1:51
N8tiv28-Apr-13 1:51 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
NotPolitcallyCorrect28-Apr-13 2:10
NotPolitcallyCorrect28-Apr-13 2:10 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Dave Kreskowiak28-Apr-13 4:48
mveDave Kreskowiak28-Apr-13 4:48 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 1:47
harold aptroot28-Apr-13 1:47 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 2:00
N8tiv28-Apr-13 2:00 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 2:23
harold aptroot28-Apr-13 2:23 
Close enough. The decrementing itself really happens while that expression is being evaluated, but that rarely matters. It does matter if you do this:
C#
int x = 0;
int z = x++ + ++x;
After that, clearly x is 2, because it was incremented twice.

And z is also 2, proving that the increment really happens while the expression is evaluated. First x++ is evaluated, the result is 0 and the effect is that x is now 1. Then ++x is evaluated, x was 1 and for pre-increment the result is the value after the increment, so the result is two (the effect is again that x is incremented, of course). So that works out to 0 + 2.

The left-to-right rules goes even further, if you have an assignment where there's an increment on the left hand side (yes, you can do that), it happens before the right hand side is evaluated, which can be demonstrated with something like this:
C#
int[] array = new int[2];
int x = 0;
array[x++] = x++;
After that, x is 2, obviously, and array looks like this: { 1, 0 }
What happened, was the first x++ was evaluated first, making the index into the array 0, then the right hand side is evaluated, which is 1 due to the first increment, and then x is incremented again, and finally the value of the right hand side (which was 1) is stored in the array at index 0.
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 2:35
N8tiv28-Apr-13 2:35 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 3:05
N8tiv28-Apr-13 3:05 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 3:33
harold aptroot28-Apr-13 3:33 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 3:44
mveOriginalGriff28-Apr-13 3:44 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 3:54
harold aptroot28-Apr-13 3:54 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:15
mveOriginalGriff28-Apr-13 4:15 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:19
harold aptroot28-Apr-13 4:19 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:26
mveOriginalGriff28-Apr-13 4:26 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:31
harold aptroot28-Apr-13 4:31 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:46
mveOriginalGriff28-Apr-13 4:46 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:59
harold aptroot28-Apr-13 4:59 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:08
mveOriginalGriff28-Apr-13 5:08 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 5:21
mvaKenneth Haugland28-Apr-13 5:21 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:25
mveOriginalGriff28-Apr-13 5:25 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 5:34
mvaKenneth Haugland28-Apr-13 5:34 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:41
mveOriginalGriff28-Apr-13 5:41 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 6:14
mvaKenneth Haugland28-Apr-13 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.