Re: Struct vs Class
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 30 Oct 2006 15:52:11 -0800
Martin Z wrote:
Anyhow, I think the reason DictionaryEntry is mutable is just because
people got really sick of saying new DictionaryEntry<MyKeyType,
MyValueType> - not for any good technical reason. I think it's
hilarious that the structs have the nice immediate-setting semantics
for mutable structs
myStructClass foo;
foo.bar = 1;
foo.baz = 2;
//all parameters initialized, it's ready to use, no constructor
needed.
but you can only use them with mutable structs anyways, making it
completely useless since MS says to never-ever-do-mutable-structs.
Do they? I've never seen them say this, but then I delve into the MSDN
doc only when I have to.
In fact, MS made some more famous mutable structs: Point and Rectangle.
I think that I understand why they did it: they did it so that people
could use syntax like this:
myPoint.X = 7;
rather than forcing them to create a whole new Point just to change the
X or Y coordinate, like this:
Point myPoint = new Point(7, myPoint.Y);
Yes, the former looks cleaner, but IMHO making Point mutable wasn't
worth all of the confusion it causes, and they would have been better
off to force us to use the second syntax.
I'm sure you know about the confusion: non-intuitive behaviour when a
property returns a Point, or when a Point is boxed for storage in an
aggregate structure. Many, many newbies try to use the above p.X = 7
syntax in those situations and then wonder why their point's
coordinates didn't change. Yuck.
I just plain don't indulge in creating mutable structs for questionable
improvements in syntax. If you want to change one aspect of a struct's
state, "new" up a new one. You just have to be sure to include a
constructor that takes all publicly-settable state as arguments.
.
- Follow-Ups:
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- References:
- Struct vs Class
- From: JohnGoogle
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- From: JohnGoogle
- Re: Struct vs Class
- From: Bruce Wood
- Re: Struct vs Class
- From: Martin Z
- Re: Struct vs Class
- From: Bruce Wood
- Re: Struct vs Class
- From: Dave Sexton
- Re: Struct vs Class
- From: Martin Z
- Struct vs Class
- Prev by Date: Re: Easy VB.NET > C# port question
- Next by Date: Re: Wrapping up software
- Previous by thread: Re: Struct vs Class
- Next by thread: Re: Struct vs Class
- Index(es):
Relevant Pages
|
Loading