is this a boxing or precedence problem?

  • Thread starter Thread starter philipl
  • Start date Start date
P

philipl

hello, with the below code, i have been trying to figure for a while
whats causing this problem. Is this a problem or feature??

i know that j is incremented below with any temp variables but i uses
a temp var. why is i printed as 0? Is it because the one in the heap
is being referenced? If so why is this, as i thought values types are
kept in the stack?

thx for expertise.

int i=0;
int j=0;
public box()
{

Console.WriteLine(++j); //this will print j as 1

Console.WriteLine(i++); //this will print i as 0
}
 
Console.WriteLine(++j); //this will print j as 1

'j' has been pre-incremented here, so its value is one when the WriteLine
method is called.
Console.WriteLine(i++); //this will print i as 0

'i' has been post-incremented, so the parameter is supplied as zero and then
i is incremented. That's just how pre and post increment operators work.

n!
 
Back
Top