Re: c# Post-incrementing problem
Yes, I agree that for the x++ portion, x will take on the
value of x before the statement (ie. x=0), but my
understanding was that x++ has an implicit assignment. In
other words, I thought x++ is equivalent to x=x+1
Regardless, it seems wrong to me, but may be right
according to the c# spec.
[color=blue]
>-----Original Message-----
>According to the C# language specification, in 14.5.9[/color]
Paragraph 5,[color=blue]
>line 2, states:
>
>"The result of x++ or x--is the value of x before the[/color]
operation,[color=blue]
>whereas the result of ++x or --x is the value of x after[/color]
the[color=blue]
>operation."
>
>You can check this out yourself:
>
>int x=0;
>MessageBox.Sho w(x++.ToString( )); //shows "0"
>
>And the reason "x=x++;" doesn't do anything is because[/color]
assignment[color=blue]
>occurs before increment (hence post-*), and the *value* of[/color]
x is[color=blue]
>increased, but is not stored (as assignment has already[/color]
occured).[color=blue]
>
>Austin Ehlers
>
>
>On Wed, 10 Sep 2003 12:48:25 -0700, "Patrick Wood"
><pwood-nospam@boeing.c om> wrote:
>[color=green]
>>I found a problem with C# and post increments. I was[/color][/color]
going[color=blue][color=green]
>>through some source code in c++ and found someone did a
>>post increment:
>>
>>int x=0;
>>for(int i=0; i<10; i++)
>>{
>>x = x++;
>>}
>>
>>In c++, you'd get 0,1,...,9 (as expected). This doesn't
>>work in C#. In C#, you get 0,...,0. It seems the post
>>increment is ignored in this case. However, the lines:
>>x=4;
>>y=x++;
>>produces y=4, x=5 (as expected)
>>
>>Should c# be behaving this way or is there a compiler
>>error? After looking at the assembly code in c++ and c#,[/color][/color]
it[color=blue][color=green]
>>looks like a bug in c#.[/color]
>
>.
>[/color]
Yes, I agree that for the x++ portion, x will take on the
value of x before the statement (ie. x=0), but my
understanding was that x++ has an implicit assignment. In
other words, I thought x++ is equivalent to x=x+1
Regardless, it seems wrong to me, but may be right
according to the c# spec.
[color=blue]
>-----Original Message-----
>According to the C# language specification, in 14.5.9[/color]
Paragraph 5,[color=blue]
>line 2, states:
>
>"The result of x++ or x--is the value of x before the[/color]
operation,[color=blue]
>whereas the result of ++x or --x is the value of x after[/color]
the[color=blue]
>operation."
>
>You can check this out yourself:
>
>int x=0;
>MessageBox.Sho w(x++.ToString( )); //shows "0"
>
>And the reason "x=x++;" doesn't do anything is because[/color]
assignment[color=blue]
>occurs before increment (hence post-*), and the *value* of[/color]
x is[color=blue]
>increased, but is not stored (as assignment has already[/color]
occured).[color=blue]
>
>Austin Ehlers
>
>
>On Wed, 10 Sep 2003 12:48:25 -0700, "Patrick Wood"
><pwood-nospam@boeing.c om> wrote:
>[color=green]
>>I found a problem with C# and post increments. I was[/color][/color]
going[color=blue][color=green]
>>through some source code in c++ and found someone did a
>>post increment:
>>
>>int x=0;
>>for(int i=0; i<10; i++)
>>{
>>x = x++;
>>}
>>
>>In c++, you'd get 0,1,...,9 (as expected). This doesn't
>>work in C#. In C#, you get 0,...,0. It seems the post
>>increment is ignored in this case. However, the lines:
>>x=4;
>>y=x++;
>>produces y=4, x=5 (as expected)
>>
>>Should c# be behaving this way or is there a compiler
>>error? After looking at the assembly code in c++ and c#,[/color][/color]
it[color=blue][color=green]
>>looks like a bug in c#.[/color]
>
>.
>[/color]
Comment