Weird result of incrementor?

  • Thread starter Thread starter kelmen
  • Start date Start date
K

kelmen

Try the below simple program.
It give expected result.

1
1
2
2

Then toggle the statements in private static int sinc(), to use
Test._num ++ .
The result:

0
0
1
1

is this result acceptable? Its not expected by me.

static void Main(string[] args)
{
Test obj1 = new Test();
Console.WriteLine("obj1.Inc(): {0}", obj1.Inc());
Console.WriteLine("obj1.Num: {0}", obj1.Num);

Test obj2 = new Test();
Console.WriteLine("obj2.Inc(): {0}", obj2.Inc());
Console.WriteLine("obj2.Num: {0}", obj2.Num);
}

public class Test
{
public int Num = 0;

private static int _num = 0;

public int Inc()
{
this.Num = Test.sinc();
return this.Num;
}

private static int sinc()
{ // toggle below statements
//return Test._num ++;
return ++ Test._num;
}
}
 
Try the below simple program.
It give expected result.

1
1
2
2

Then toggle the statements in private static int sinc(), to use
Test._num ++ .
The result:

0
0
1
1

is this result acceptable? Its not expected by me.

Can I ask you what you were expecting, and why? What is your
understanding of the difference between prefix ++ and postfix ++ ?

[code quoted for reference]
static void Main(string[] args)
{
Test obj1 = new Test();
Console.WriteLine("obj1.Inc(): {0}", obj1.Inc());
Console.WriteLine("obj1.Num: {0}", obj1.Num);

Test obj2 = new Test();
Console.WriteLine("obj2.Inc(): {0}", obj2.Inc());
Console.WriteLine("obj2.Num: {0}", obj2.Num);
}

public class Test
{
public int Num = 0;

private static int _num = 0;

public int Inc()
{
this.Num = Test.sinc();
return this.Num;
}

private static int sinc()
{ // toggle below statements
//return Test._num ++;
return ++ Test._num;
}
}
 
It is exactly what I would expect to see, the pre increment operator
increments and then returns the incremented value, while the post increment
operator returns the original value and then increments it. What did you
expect.
 
I know the pre and post increment stuff.

But what pickle me is the logic and flow:
- the increment is done within a routine -- Test.sinc()
- this.Num value should be from the routine, so in my logic, regardless
its pre or post, the value returned should be incremented already.
- to me, the effect of the pre and post should only be affective inside
the routine, should not affect the result returning to the calling.

I did a debug trace, and put on a watch on the this.Num and Test._num.
And you will be very surprise of the findout:
- during the line [return Test._num ++], Num is 0, _num is 0
- next step, back to the calling line, _num is 1 (red), Num is still 0
- next step, still same _num is 1 (black), Num is still 0
(red)...????!!!!

funny?

tony said:
It is exactly what I would expect to see, the pre increment operator
increments and then returns the incremented value, while the post increment
operator returns the original value and then increments it. What did you
expect.

Try the below simple program.
It give expected result.

1
1
2
2

Then toggle the statements in private static int sinc(), to use
Test._num ++ .
The result:

0
0
1
1

is this result acceptable? Its not expected by me.

static void Main(string[] args)
{
Test obj1 = new Test();
Console.WriteLine("obj1.Inc(): {0}", obj1.Inc());
Console.WriteLine("obj1.Num: {0}", obj1.Num);

Test obj2 = new Test();
Console.WriteLine("obj2.Inc(): {0}", obj2.Inc());
Console.WriteLine("obj2.Num: {0}", obj2.Num);
}

public class Test
{
public int Num = 0;

private static int _num = 0;

public int Inc()
{
this.Num = Test.sinc();
return this.Num;
}

private static int sinc()
{ // toggle below statements
//return Test._num ++;
return ++ Test._num;
}
}
 
What is happening is exactly what should happen, Num is only set by the value
returned and that is returned as 0, only when the routine is called a second
time do you see the new value, that was generated after the original number
was returned.

I know the pre and post increment stuff.

But what pickle me is the logic and flow:
- the increment is done within a routine -- Test.sinc()
- this.Num value should be from the routine, so in my logic, regardless
its pre or post, the value returned should be incremented already.
- to me, the effect of the pre and post should only be affective inside
the routine, should not affect the result returning to the calling.

I did a debug trace, and put on a watch on the this.Num and Test._num.
And you will be very surprise of the findout:
- during the line [return Test._num ++], Num is 0, _num is 0
- next step, back to the calling line, _num is 1 (red), Num is still 0
- next step, still same _num is 1 (black), Num is still 0
(red)...????!!!!

funny?

tony said:
It is exactly what I would expect to see, the pre increment operator
increments and then returns the incremented value, while the post increment
operator returns the original value and then increments it. What did you
expect.

Try the below simple program.
It give expected result.

1
1
2
2

Then toggle the statements in private static int sinc(), to use
Test._num ++ .
The result:

0
0
1
1

is this result acceptable? Its not expected by me.

static void Main(string[] args)
{
Test obj1 = new Test();
Console.WriteLine("obj1.Inc(): {0}", obj1.Inc());
Console.WriteLine("obj1.Num: {0}", obj1.Num);

Test obj2 = new Test();
Console.WriteLine("obj2.Inc(): {0}", obj2.Inc());
Console.WriteLine("obj2.Num: {0}", obj2.Num);
}

public class Test
{
public int Num = 0;

private static int _num = 0;

public int Inc()
{
this.Num = Test.sinc();
return this.Num;
}

private static int sinc()
{ // toggle below statements
//return Test._num ++;
return ++ Test._num;
}
}
 
I know the pre and post increment stuff.

I don't think you do.
But what pickle me is the logic and flow:
- the increment is done within a routine -- Test.sinc()
- this.Num value should be from the routine, so in my logic, regardless
its pre or post, the value returned should be incremented already.

No, you're confused about what pre/post increments do.

Pre-increment means "change the value of the variable, and return the
new value"

Post-increment means "remember the value of the variable, change it,
then return the original value".

So when you use a post increment, the value of the expression is the
value before any increment occurs.

If you changed your code to:

Test._num++
return Test._num

*then* you would see the same results whether you used pre or post
increment.
- to me, the effect of the pre and post should only be affective inside
the routine, should not affect the result returning to the calling.

I did a debug trace, and put on a watch on the this.Num and Test._num.
And you will be very surprise of the findout:
- during the line [return Test._num ++], Num is 0, _num is 0
- next step, back to the calling line, _num is 1 (red), Num is still 0
- next step, still same _num is 1 (black), Num is still 0
(red)...????!!!!

Well, that's an issue with watching a property which changes values
while you're in the debugger.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top