A question on the RECT struct

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

    #1

    A question on the RECT struct

    is there a difference between the following:

    Rectangle r = new Rectangle (10,20,30,40);
    Rectangle r = Rectangle.FromL TRB(10,20,30,40 );

    Also, I guess I'm not sure why Structs are created with a NEW keyword,
    anyway. Aren't they located on the stack, like values, and isn't NEW
    used to create space on the heap?

    Dom

  • Mattias Sjögren

    #2
    Re: A question on the RECT struct

    >is there a difference between the following:
    >
    >Rectangle r = new Rectangle (10,20,30,40);
    >Rectangle r = Rectangle.FromL TRB(10,20,30,40 );
    Yes. In the first case 30 and 40 will be the width and height
    respectively of the rectangle. In the FromLRTB case it will be the
    Right and Bottom coordinates.

    >Also, I guess I'm not sure why Structs are created with a NEW keyword,
    >anyway. Aren't they located on the stack, like values, and isn't NEW
    >used to create space on the heap?
    No, using the new keyword doesn't imply heap allocation. And structs
    aren't always located on the stack.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: A question on the RECT struct

      Dom <dolivastro@gma il.comwrote:

      <snip>
      Structs are not ALWAYS located on the heap? When are they, and when
      are they not?
      See http://pobox.com/~skeet/csharp/memory.html

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Dom

        #4
        Re: A question on the RECT struct

        On Aug 20, 4:58 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        Dom <dolivas...@gma il.comwrote:
        >
        <snip>
        >
        Structs are not ALWAYS located on the heap? When are they, and when
        are they not?
        >
        Seehttp://pobox.com/~skeet/csharp/memory.html
        >
        --
        Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too
        Thanks for the article. Very helpful. The other articles at the site
        (that I've read) were also quite good.

        Comment

        • Peter Duniho

          #5
          Re: A question on the RECT struct

          Dom wrote:
          I hope I'm not a pest, but I guess the question I have is better put
          this way: Since there are already 3 overloads for the Rectangle
          constructor, why isn't there just a fourth that takes the LTRB args?
          I'm just confused about the two calls, one to a constructor, one to a
          method.
          Here's the declaration for the constructor that takes the top-left
          coordinates and the size:

          public Rectangle(int x, int y, int width, int height);

          Here's the declaration for the constructor that takes the top-left and
          bottom-right coordinates:

          public Rectangle(int xLeft, int yTop, int xRight, int yBottom);

          Here's some code that uses these constructors:

          // initialize with top-left and size:
          Rectangle rectA = new Rectangle(10, 20, 30, 30);

          // initialize with top-left and bottom-right:
          Rectangle rectB = new Rectangle(10, 20, 40, 50);

          Now, imagine you're the compiler. In the above code example, how do you
          know which constructor goes with which assignment?

          That should illustrate the issue, but just in case:

          The reason that the constructor exists for one and not the other is that
          both are method signatures that takes four integers and return a
          Rectangle. You can't have two constructors with identical signatures,
          so at least one of the methods cannot be a constructor. It has to be a
          plain static method that returns a Rectangle.

          Now, a valid question might be: why not forego the constructor
          altogether and make things consistent by making all of the
          initialization methods be plain static methods.

          IMHO, the answer to that is that a constructor really is a preferable
          way to initialize an object (it's a lot more discoverable, especially
          via Intellisense, for one), and so it's worth making as many of the
          initialization methods be constructors.

          That's one possible answer. I don't know if that's the actual reason
          for the current design, but it seems plausible to me.

          Pete

          Comment

          • Dom

            #6
            Re: A question on the RECT struct

            On Aug 20, 10:52 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.com>
            wrote:
            Dom wrote:
            I hope I'm not a pest, but I guess the question I have is better put
            this way: Since there are already 3 overloads for the Rectangle
            constructor, why isn't there just a fourth that takes the LTRB args?
            I'm just confused about the two calls, one to a constructor, one to a
            method.
            >
            Here's the declaration for the constructor that takes the top-left
            coordinates and the size:
            >
            public Rectangle(int x, int y, int width, int height);
            >
            Here's the declaration for the constructor that takes the top-left and
            bottom-right coordinates:
            >
            public Rectangle(int xLeft, int yTop, int xRight, int yBottom);
            >
            Here's some code that uses these constructors:
            >
            // initialize with top-left and size:
            Rectangle rectA = new Rectangle(10, 20, 30, 30);
            >
            // initialize with top-left and bottom-right:
            Rectangle rectB = new Rectangle(10, 20, 40, 50);
            >
            Now, imagine you're the compiler. In the above code example, how do you
            know which constructor goes with which assignment?
            >
            That should illustrate the issue, but just in case:
            >
            The reason that the constructor exists for one and not the other is that
            both are method signatures that takes four integers and return a
            Rectangle. You can't have two constructors with identical signatures,
            so at least one of the methods cannot be a constructor. It has to be a
            plain static method that returns a Rectangle.
            >
            Now, a valid question might be: why not forego the constructor
            altogether and make things consistent by making all of the
            initialization methods be plain static methods.
            >
            IMHO, the answer to that is that a constructor really is a preferable
            way to initialize an object (it's a lot more discoverable, especially
            via Intellisense, for one), and so it's worth making as many of the
            initialization methods be constructors.
            >
            That's one possible answer. I don't know if that's the actual reason
            for the current design, but it seems plausible to me.
            >
            Pete
            Got it. Thanks.

            Comment

            Working...