G
gareth erskine-jones
The following comes from "Pro VB 2005 and the .NET 2.0 Platform", APress, by
Andrew Troelsen
Page 313 (Understanding Value Types and Reference Types):
"Specifically speaking, a .NET data type may be value-based or
reference-based. Value-based types, which include all numerical data types
(Integer, Double, etc.), as well as enumerations and structures, are
allocated on the stack."
Point #1:
Realizing that this is a C# NG, but also realizing the Greg used the VB .NET
term "structure", let me quote from page 123 (Programming Constructs):
"...structures do not have an identically named class representation in the
.NET library (that is there is no System.Structure class), but are
implicitly derived from System.ValueType."
and from page 314:
"...understand that .NET structures and enumerations are also value types.
Structures, as you may recall from Chapter 4, provide a way to achieve
bare-bones benefits of object orientation (i.e. , encapsulation) while
having the efficiency of stack-allocated data."
Your statement to Greg that the term "structure" to refer to a value type
was incorrect, is itself incorrect.
Point #2:
You stated:
"Any discussion of reference types and value types that begins by mentioning
the heap and the stack is, at a minimum, bound to fall short of precision,
and at worst will be extremely misleading."
The author (who you may have heard of and is considered to be quite an
expert in .NET) explains value-types to the reader in just the same way I
did. In fact, this approach is also taken in "Professional Visual Basic
2008", WROX. And, in fact, it is taken in just about every text I can find
on the subject.
I agree that the vast majority of books & documentation on this
subject do seem to follow the line you advocate, but I have to agree
with Peter that it is a misleading line to follow. I think it's
particularly revealing that in all of the discussion above, the real
semantic difference between value and reference types is not
mentioned.
It's perfectly possible for a programmer to use value and reference
types in c# without him being taught about the stack and the heap -
but if he doesn't know that when passing the former, a copy is made
(ie. it is passed by value) and when passing the latter, no copy is
made (it is passed by reference), then his code will be very
defective.
I don't think this is just a hypothetical situation. I can't, off
hand, remember when I saw a performance problem which had to be fixed
by switching from a reference type to a value type (obviously, this
does sometimes matter - I just don't think it happens very much). I
have seen lots of cases where there were subtle errors in code because
the passing semantics were not fully understood (e.g. implementing
Equals() on a value type is very different to a reference type).
Despite what you may personally belive, I (and just a few notable others)
feel that it is appropriate to discuss reference and value types in the
context of the stack and the heap and that it is not necessary to explore
all the gory details during the initial exposure to the concepts.
The clincher for me is that it's possible to explain all of the
semantic differences between the two without ever mentioning the stack
or heap, which suggests that it's actually the details of where they
are allocated which are the unnecessary gory details. Indeed - as Eric
Lippert has pointed out, it's perfectly possible to implement the CLI
without implementing value types on the stack.
GSEJ