Re: Struct vs Class




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.

.



Relevant Pages

  • Re: Struct vs Class
    ... I've been reading the C# Language Specification for Structs and found ... Given the declaration ... why does the example in the language specification provide public ... System.Drawing.Rectangle are mutable structs. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cant assign value to structure in array?
    ... Note that it makes a big difference whether it is an array: ... ArrayList arr = new ArrayList; ... the structs are boxed and references to ... mutable structs is just asking for trouble. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: textbook authors: passing by ref is NOT more efficient!
    ... sthat contains structs you can't return the structs by a property and have ... Ah, so you not only want return by reference, but you specifically want ... Aargh, aargh, aargh. ... Mutable structs are to be avoided like the plague. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [LogoForum] "struct" type in Logo?
    ... The syntax and all the primitives stay unchanged. ... I'm not against structs or speaking more globally I'm not against Logo to ...
    (comp.lang.logo)
  • exporting function with parm pointer to struct
    ... I'm exporting a C-style function with this syntax: ... 4_PARM is a structure declared in a proprietary header file that cannot be ... there are several more Structs similar to 4_PARM. ...
    (microsoft.public.dotnet.languages.csharp)

Loading