c# Post-incrementing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pat

    #16
    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]

    Comment

    • Jon Skeet

      #17
      Re: c# Post-incrementing problem

      Pat <pwood-nospam@boeing.c om> wrote:[color=blue]
      > 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.[/color]

      x++ has an implicit assignment to x, yes. However, that's effectively
      overridden by the later assignment to x.
      [color=blue]
      > In other words, I thought x++ is equivalent to x=x+1[/color]

      Yes, it is - but the value of the expression is the *original* value,
      and that's what then gets assigned by the "x=" part of x=x++;
      [color=blue]
      > Regardless, it seems wrong to me, but may be right
      > according to the c# spec.[/color]

      Basically remember that the increment happens immediately the
      expression is evaluated, *not* after the whole statement is evaluated.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jack Hanebach

        #18
        Re: c# Post-incrementing problem

        "Pat" <pwood-nospam@boeing.c om> wrote in message
        news:072e01c378 57$eab600b0$a50 1280a@phx.gbl.. .[color=blue]
        > 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]

        Look at it this way:

        x = x++; is equivalent to x = x.operator++();

        where int.operator++( ) translates to something like

        {
        int temp = this;
        this = this + 1;
        return temp;
        }

        Now the behaviour you see makes sense, doesn't it.


        Comment

        • Stu Smith

          #19
          Re: c# Post-incrementing problem

          This is the crucial point, and one I was just about to make.

          People assume if their C++ compiler does something, then every other C++
          compiler should do the same.

          I'm not sure why you'd want to do this in C# anyway, it's absolutely
          horrible code.


          "David Olsen" <qg4h9ykc5m@yah oo.com> wrote in message
          news:uMyvY4%23d DHA.3096@TK2MSF TNGP11.phx.gbl. ..[color=blue]
          > Patrick Wood wrote:[color=green]
          > > I found a problem with C# and post increments. I was going
          > > 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).[/color]
          >
          > In C++, the behavior of this code is undefined. 'x' is modified twice
          > between sequence points (once by ++, once by =), so the C++ standard
          > makes no guarantees about the code's behavior. You can't expect the
          > code to do anything in particular.
          >
          > --
          > David Olsen
          > qg4h9ykc5m@yaho o.com
          >[/color]


          Comment

          Working...