about struct (e.g. System.Drawing.Point)

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

    about struct (e.g. System.Drawing.Point)

    Point is a struct
    but I can do both

    Point a;
    Write(a.X);
    Write(a.Y);

    and
    Point a = new Point(1,1);
    Write(a.X);
    Write(a.Y);
    .....seems confusing!


  • Jon Skeet

    #2
    Re: about struct (e.g. System.Drawing. Point)

    Action <amuro@hotmail. com> wrote:[color=blue]
    > Point is a struct
    > but I can do both
    >
    > Point a;
    > Write(a.X);
    > Write(a.Y);[/color]

    Yes, although it's a bad idea to do that - I'd always write the above
    as

    Point a = new Point();
    Write (a.X);
    Write (a.Y);

    It'll do the same thing (always, I believe) but it means that the whole
    of the struct is definitely assigned.
    [color=blue]
    > and
    > Point a = new Point(1,1);
    > Write(a.X);
    > Write(a.Y);
    > ....seems confusing![/color]

    Why? Basically "new" returns a new *value* for value-types, not a new
    reference.

    --
    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

    • Action

      #3
      Re: about struct (e.g. System.Drawing. Point)

      Does this mean that
      Point a = new Point(1,2);
      Write (a.X);
      Write (a.Y);
      1. a new Point is created
      2. Value of the newly created Point is assigned to Point "a"
      3. the "new Point" is destroyed, as Point "a" is a struct, it didn't really
      points to the "new Point", so the "new Point" has zero reference => GC clean
      it up....

      is this the case?
      thx!

      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19db68 d27e7b99b989741 @news.microsoft .com...[color=blue]
      > Action <amuro@hotmail. com> wrote:[color=green]
      > > Point is a struct
      > > but I can do both
      > >
      > > Point a;
      > > Write(a.X);
      > > Write(a.Y);[/color]
      >
      > Yes, although it's a bad idea to do that - I'd always write the above
      > as
      >
      > Point a = new Point();
      > Write (a.X);
      > Write (a.Y);
      >
      > It'll do the same thing (always, I believe) but it means that the whole
      > of the struct is definitely assigned.
      >[color=green]
      > > and
      > > Point a = new Point(1,1);
      > > Write(a.X);
      > > Write(a.Y);
      > > ....seems confusing![/color]
      >
      > Why? Basically "new" returns a new *value* for value-types, not a new
      > reference.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet

        #4
        Re: about struct (e.g. System.Drawing. Point)

        Action <amuro@hotmail. com> wrote:[color=blue]
        > Does this mean that
        > Point a = new Point(1,2);
        > Write (a.X);
        > Write (a.Y);
        > 1. a new Point is created
        > 2. Value of the newly created Point is assigned to Point "a"
        > 3. the "new Point" is destroyed, as Point "a" is a struct, it didn't really
        > points to the "new Point", so the "new Point" has zero reference => GC clean
        > it up....
        >
        > is this the case?[/color]

        Not quite - because Point is a value type, there *is* nothing on the
        heap to be cleaned up. It's just the same as doing:

        int a=5;
        int b=10;
        int x = a+b;

        - the addition is performed, and the value is assigned to x. Exactly
        the same kind of thing is happening here - the point is created, and
        then assigned to the variable. At no point does anything need to be on
        the heap - there are no references involved.

        --
        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

        Working...