On Sat, 31 May 2008 19:03:37 -0700, Ray Cassick
[quoted text, click to view] <rcassick@enterprocity.com> wrote:
> [...]
> 16 bytes just seemed really small. I use structures a lot anytime I need
> to
> pass data across functional boundaries but don't need to give it much
> functionality that would require a class type structure.
IMHO, the more often you "cross functional boundaries", the more a class
becomes more appropriate than a struct.
Size of the object isn't the only factor. There are fundamental
differences in the way that the data structures are used. Structs are
value types and as such, unless they are boxed (which carries its own
penalty), every time you pass a struct from one place to the other, you
are copying the entire struct. A class is a reference type, and is passed
simply by copying the reference to the instance, not all of the data.
(Yes, you could always pass by reference, but that's abusing the language
and if nothing else adds a pointless architectural requirement on any
method that uses the data structure).
If all you're doing is storing a bunch of related data and passing it
around, a simple immutable class may be what you want, assuming it's a lot
of data.
[quoted text, click to view] > Like if I want to
> just write out a series of records to a file I will build the record
> structure, populate it and then write it out. Sometimes just writing out
> to
> a stream using a structure is so much simpler than serializing something,
> especially if I just need it written out to something simple for
> consumption
> in another app. XML is good but you can cross the line and just induce a
> ton
> of overhead if you are not careful.
IMHO, for that application, it seems to me that using a BinaryWriter is
better, rather than depending on the layout of a struct. It may actually
perform better too, since you theoretically would copy each part of the
data one fewer time (though I admit that if you're doing i/o, that may not
be a consequential difference).
[quoted text, click to view] > I was just worried that by creating structures too large I may be
> inducing a
> performance hit if there was some magic limit between being stored on the
> stack vs. the heap that I was not aware of.
There is no magic limit that I know of. That doesn't mean it doesn't
exist; I'm not an expert on the subject.
What I do know is that while there's no magic limit (that I know of),
structs _do_ incur overhead related to the copying of the data within the
struct that classes don't, and as the struct gets larger, this copying
starts to become consequential.