Re: Object "Literals"
- From: "Zanna" <znt.fabio@xxxxxxxxxxx>
- Date: Sat, 20 Aug 2005 17:34:01 GMT
"Sue & Bill" <sue.and.bill@xxxxxxxxx> ha scritto nel messaggio
> Rectangle[] rects = new Rectangle[10000];
>
>
> I want to initialize the values of rects as follows:
>
> Option A:
> ========
> for (int i=0; i<rects.Length; i++)
> {
> rects[i].X = 0;
> rects[i].Y = 0;
> rects[i].Size.Width = 100;
> rects[i].Size.Height = 20;
> }
>
> (By the way, rects[i].Size.Width = 100 above generates a compiler
> error. I haven't figured out why.)
Because the implementation of Rectangle.Size is
public Size Size
{
get { return new Size(this.Width, this.Height); }
set {[snip]}
}
So you cannot assign a value to a "new Size".
You need to use the "set" part:
rects[i].Size = new Size(100, 20);
> What is the impact on memory or performance, if any, if I use the
> following instead:
>
> Option B:
> ========
> for (int i=0; i<rects.Length; i++)
> rects[i]=new Rectangle(new Point(0,0), new Size(100,20));
>
> Option C:
> ========
> for (int i=0; i<rects.Length; i++)
> {
> rects[i].Location = new Point(0,0);
> rects[i].Size = new Size(100,20);
> }
>
Here it is the implementation of Rectangle.ctor(Point, Size)
public Rectangle(Point location, Size size)
{
this.x = location.X;
this.y = location.Y;
this.width = size.Width;
this.height = size.Height;
}
So if you use Rectangle.ctor(int, int, int, int) you avoid the two "new" on
Size and Point.
The same on C).
--
Reporting tool: http://www.neodatatype.net
.
- Follow-Ups:
- Re: Object "Literals"
- From: Zanna
- Re: Object "Literals"
- References:
- Object "Literals"
- From: Sue & Bill
- Object "Literals"
- Prev by Date: Re: Thread Safety - WinForms
- Next by Date: Re: Object "Literals"
- Previous by thread: Re: Object "Literals"
- Next by thread: Re: Object "Literals"
- Index(es):
Relevant Pages
|