Re: Structs vs. Classes
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sun, 6 May 2007 19:31:41 +0100
Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
Well, it does the same thing in some ways, and different things in
other ways. It does the "same thing" in the same way that both value
types and reference types are passed by value by default
Yes, that's sort of true. And it does the "same thing" in the same way
that "Control.Invoke" and "Delegate.Invoke" do the "same thing". My point
is that in the case of the "new" operator and the "Invoke" method, the
"same thing" is different enough that using the same syntax obscures some
fundamental differences in what's actually happening.
I'd say they're more similar than that - but it's the kind of thing
which wouldn't do much good to debate.
(For the record, the parameter passing aspect doesn't bother me at all,
because I think one thing that C# does make clear is the difference
between representation of value types and reference types).
Shame it's something that gets a lot of people confused :(
- if you
understand that the difference between reference types and value types,
it makes sense, although if you don't, it doesn't.
Well, I *think* I understand the difference, and yet to me it doesn't make
sense to have to call "new" on a value type.
For me it creates a new "value" - but there we go.
[...] The
"new" operator doesn't really create the instance that you're using.
I think it depends on exactly what one means by "instance", to be
honest. I nearly used the word "value" but thought that wouldn't be
clear. It's quite possible that we don't have adequate terminology at
the moment.
Maybe. To me, "instance" means the actual storage required by the
variable. There's a physical, real-world connection between an "instance"
and the place where that "instance" is stored. But with value types in
C#, by the time I get around to writing "ValueType variable = new
ValueType()", the storage has been allocated and all that "new" is doing
for me is initializing that storage.
You see, to me, an instance is a collection of variable values. The
storage doesn't particularly bother me. Therefore even if I've already
got a value, with my way of looking at things I'm creating a new
instance which is replacing the old instance - whereas of course with
your way of of looking at it I quite see that you're just replacing the
data within the existing instance.
I suspect we'll have to agree to disagree on this, but I'm fine with
that.
Me too. After all, it doesn't change how we use the language. I just
feel it's more confusion to new C# programmers than it needs to be. (And
personally, being a new C# programmer I feel that I have a better handler
on what's confusing and what's not than some of you more experienced C#
folks :) ).
That would certainly be true if I hadn't helped rather a lot of other
newbie C# people over time (not trying to be immodest, just honest) -
and I think you have *much* more experience of other languages than
most of the C# neophytes I've seen, to be honest. I expect to be able
to explain things to you (on the rare occasions where it's necessary,
frankly) in terms which are defined elsewhere, point you at those
definitions and let you get on with it - rather than doing a lot of
hand-holding, comparing with real-world examples etc.
This is in no way to diminish the importance of the things you find
irritating/tricky/confusing/whatever.
Out of interest, what would you propose for calling a value type
constructor? (Please not the C++ syntax!)
Off the top of my head, I don't know. There's a reason I'm not a language
designer. :) All I know is that this code:
ValueType valuevar = new ValueType();
RefType refvar = new RefType();
has two lines that look like they do the same thing, and yet they don't do
the same thing at all.
Wise words from Obi wan: "Luke, you're going to find that many of the
truths we cling to depend greatly on our own point of view."
:)
They're certainly different in terms of whether they need to allocate
storage or not. If you see the major point as being initializing an
instance, and use my view of an instance, it's a different story :)
(I'm not going to claim the differences go away, just that I'd disagree
with the claim that "they don't do the same thing at all".)
If there were some way to get a reference to a value type that took
advantage of the "new" syntax, maybe it wouldn't bug me so much.
Something related to boxing, for example. But as far as I know, there
isn't. Feel free to correct me if I'm wrong about that. :)
Well, this would do it:
object o = new ValueType();
but it's really not what you want, most of the time ;)
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Structs vs. Classes
- From: Peter Duniho
- Re: Structs vs. Classes
- References:
- Structs vs. Classes
- From: Dom
- Re: Structs vs. Classes
- From: Diego Jancic
- Re: Structs vs. Classes
- From: Arne Vajhøj
- Re: Structs vs. Classes
- From: Dom
- Re: Structs vs. Classes
- From: Peter Duniho
- Re: Structs vs. Classes
- From: Jon Skeet [C# MVP]
- Re: Structs vs. Classes
- From: Peter Duniho
- Re: Structs vs. Classes
- From: Jon Skeet [C# MVP]
- Re: Structs vs. Classes
- From: Peter Duniho
- Structs vs. Classes
- Prev by Date: Re: memcpy functionality? please help!
- Next by Date: Re: Getting info from excel.
- Previous by thread: Re: Structs vs. Classes
- Next by thread: Re: Structs vs. Classes
- Index(es):
Relevant Pages
|