469,625 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

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!
Nov 15 '05 #1
3 3644
Action <am***@hotmail.com> wrote:
Point is a struct
but I can do both

Point a;
Write(a.X);
Write(a.Y);
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.
and
Point a = new Point(1,1);
Write(a.X);
Write(a.Y);
....seems confusing!


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
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" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. ..
Action <am***@hotmail.com> wrote:
Point is a struct
but I can do both

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


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.
and
Point a = new Point(1,1);
Write(a.X);
Write(a.Y);
....seems confusing!


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Action <am***@hotmail.com> wrote:
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?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.