By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

A question on the RECT struct

P: n/a
Dom
is there a difference between the following:

Rectangle r = new Rectangle (10,20,30,40);
Rectangle r = Rectangle.FromLTRB(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

Aug 20 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
>is there a difference between the following:

Rectangle r = new Rectangle (10,20,30,40);
Rectangle r = Rectangle.FromLTRB(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.
Aug 20 '07 #2

P: n/a
Dom <do********@gmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 20 '07 #3

P: n/a
Dom
On Aug 20, 4:58 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Dom <dolivas...@gmail.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.com>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.

Aug 21 '07 #4

P: n/a
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
Aug 21 '07 #5

P: n/a
Dom
On Aug 20, 10:52 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.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.

Aug 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.