Re: Struct vs Class
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Mon, 30 Oct 2006 12:57:20 -0500
Hi Bruce,
Of course, structs that contain references to _mutable_
objects can lead to some awfully puzzling, nearly-unpredictable
behaviour, so I wouldn't recommend that. :-)
DictionaryEntry is one that doesn't conform to that idea, however.
--
Dave Sexton
"Bruce Wood" <brucewood@xxxxxxxxxx> wrote in message
news:1162229711.383330.47510@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Martin Z wrote:
Bruce, My understanding is that, if designed to be entirely immutable
and containing no events, then there is almost no visible difference
between a class and a struct.
Apparently so. I'm trying to think of differences in behaviour between
an immutable class and a struct, and I can't. Of course, it's Monday
morning. I could be wrong.
One would have to overload == appropriately (and, one presumes, the
other mathematical and comparison operators), but apart from that I
can't think of any hitches. Of course, the temptation with a class
would be to make it mutable, but that's just a temptation, not a given.
The struct is primarily a speed-optimization - access to stack-variables is
much faster.
I wouldn't state that as the primary speed benefits of structs. I
consider the primary speed benefit of structs being that they don't
need to be garbage-collected. So, if you are doing extensive
calculations that involve the creation of myriad intermediate results
then structs are a better choice because objects created on the heap
have to be GC'd.
However, this is only one consideration in whether to make something a
struct or a class, and not even the most important. The most important
consideration, IMHO, is whether the thing you want to create displays
_value semantics_. There are lots of discussions in this newsgroup
about struct vs class and value vs reference semantics.
Hence, structs should only contain other valuetypes.
No, I disagree. For example, I have a struct called a Measure, which
contains a decimal quantity and a unit of measure. UnitOfMeasure is a(n
immutable) reference type. I think that our disagreement on this point
goes back to the speed benefits of structs: I see storage on the stack
as a side-effect of using structs, not as a motivation for using
structs, and so I don't see anything wrong with structs that contain
references. Of course, structs that contain references to _mutable_
objects can lead to some awfully puzzling, nearly-unpredictable
behaviour, so I wouldn't recommend that. :-)
.
- Follow-Ups:
- Re: Struct vs Class
- From: Martin Z
- 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
- Struct vs Class
- Prev by Date: Re: How do I check to see if a dataset has a DBNull value
- Next by Date: Convert C# to C++
- Previous by thread: Re: Struct vs Class
- Next by thread: Re: Struct vs Class
- Index(es):
Relevant Pages
|