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

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 22:35
N8tiv28-Apr-13 22:35 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 22:42
harold aptroot28-Apr-13 22:42 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 22:54
N8tiv28-Apr-13 22:54 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 23:39
mveOriginalGriff28-Apr-13 23:39 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 23:47
N8tiv28-Apr-13 23:47 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff29-Apr-13 0:33
mveOriginalGriff29-Apr-13 0:33 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv30-Apr-13 19:04
N8tiv30-Apr-13 19:04 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff30-Apr-13 21:57
mveOriginalGriff30-Apr-13 21:57 
No, no - sorry, I'm probably confusing you. Let's go right back to the beginning.
Assume we have a function with just a two lines of code:
C#
int x = 10;
Console.WriteLine(x);

If we run it, we obviously get "10" printed. So let's start with that and add bits between the two lines.
C#
int x = 10;
x++;
Console.WriteLine(x);
Will print 11, because the post increment adds one to the value of x
C#
int x = 10;
++x;
Console.WriteLine(x);
Will also print 11, because the pre increment adds one to the value as well.
Prefix and postfix operations always affect the value of the variable; the only difference is when the variable is altered.
With prefix, the variable is altered before it's value is used: So
C#
int x= 10;
int y = ++x;
Console.WriteLine("x={0}, y = {1}", x, y);
will give us "x = 11, y = 11" because x was altered before it's value was used in the expression. This is the equivalent of writing:
C#
int x = 10;
x = x + 1;
int y = ++x;
Console.WriteLine("x={0}, y = {1}", x, y);
With post fix, the variable is altered after its's value is used in the expression:
C#
int x = 10;
int y = x++;
Console.WriteLine("x={0}, y = {1}", x, y);
Will give us "x=11, y = 10" and is the equivalent of writing:
C#
int x= 10;
int y = x;
x = x + 1;
Console.WriteLine("x={0}, y = {1}", x, y);


Does that make sense so far?
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv30-Apr-13 23:02
N8tiv30-Apr-13 23:02 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff30-Apr-13 23:36
mveOriginalGriff30-Apr-13 23:36 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv4-May-13 22:31
N8tiv4-May-13 22:31 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff4-May-13 22:50
mveOriginalGriff4-May-13 22:50 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv4-May-13 23:02
N8tiv4-May-13 23:02 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff4-May-13 23:14
mveOriginalGriff4-May-13 23:14 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv4-May-13 23:48
N8tiv4-May-13 23:48 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff4-May-13 23:58
mveOriginalGriff4-May-13 23:58 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:04
N8tiv5-May-13 0:04 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff5-May-13 0:11
mveOriginalGriff5-May-13 0:11 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:17
N8tiv5-May-13 0:17 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:19
N8tiv5-May-13 0:19 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:05
N8tiv5-May-13 0:05 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff5-May-13 0:21
mveOriginalGriff5-May-13 0:21 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv5-May-13 0:24
N8tiv5-May-13 0:24 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff5-May-13 0:28
mveOriginalGriff5-May-13 0:28 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kevin Bewley29-Apr-13 0:30
Kevin Bewley29-Apr-13 0:30 

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.