Object "Literals"

S

Sue & Bill

I have:

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.X = 0;
rects.Y = 0;
rects.Size.Width = 100;
rects.Size.Height = 20;
}

(By the way, rects.Size.Width = 100 above generates a compiler
error. I haven't figured out why.)

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=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects.Location = new Point(0,0);
rects.Size = new Size(100,20);
}


Thank you for your help.
 
R

Richard Blewett [DevelopMentor]

A Rectangle is a value type and so when you index into the array you get back a copy. The compiler is trying to prevent you from doing something you are not intending - updating a copy of the array memeber not the array member itself. Option B is the only one that wil actually do what you want.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I have:

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.X = 0;
rects.Y = 0;
rects.Size.Width = 100;
rects.Size.Height = 20;
}

(By the way, rects.Size.Width = 100 above generates a compiler
error. I haven't figured out why.)

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=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects.Location = new Point(0,0);
rects.Size = new Size(100,20);
}


Thank you for your help.


[microsoft.public.dotnet.languages.csharp]
 
Z

Zanna

Sue & Bill said:
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.X = 0;
rects.Y = 0;
rects.Size.Width = 100;
rects.Size.Height = 20;
}

(By the way, rects.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.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=new Rectangle(new Point(0,0), new Size(100,20));

Option C:
========
for (int i=0; i<rects.Length; i++)
{
rects.Location = new Point(0,0);
rects.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).
 
S

Sue & Bill

Thanks all for the explanation of why .Size.Width cannot be assigned.
A mystery solved, much appreciated.

My main question was whether 10,000 calls of the new statement would
gobble up lots of memory. If not, is there a way to create object
"literals" to assign to an existing instance of an object?

Thanks.
 
R

Richard Blewett [DevelopMentor]

It will not allocate any more memory it will reinitialize the existing array member as Rectangle is a value type

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Thanks all for the explanation of why .Size.Width cannot be assigned.
A mystery solved, much appreciated.

My main question was whether 10,000 calls of the new statement would
gobble up lots of memory. If not, is there a way to create object
"literals" to assign to an existing instance of an object?

Thanks.


[microsoft.public.dotnet.languages.csharp]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top