Value Types and Reference Types

R

Rafael Veronezi

Just to fix, correct me if I am wrong...
With reference types (objects), if I assign an object to another, the
assignment will be the address of the object, and not a copy of it's
contents right?
With value types (structs), if I assign an object to another, I'll be
copying it's data to the left side of the assignment, and not it's address.

This is valid with method parameters too, like, if I pass an object as a
parameter, i'll be passing a reference to the memory point where the object
is allocated, and every change made to the object in the method, will be
reflected when the method returns, again with Value Types, if I pass a value
type as a method parameter, I'm passing only the reference, in this case I
need to specify out or ref keyword in the parameter, to indicate that I want
to pass it by reference...

Few questions...
With value types, if I pass an out parameter for example, I'm passing it's
memory address to, or it works with some kind of boxing/unboxing technique
to pass these parameters? It's was just a thought, don't know if it make
sense, because I don't think that it will need to box the value type to pass
that as a reference... anyways...

I think it's that..
Thanks guys!
 
J

Jon Skeet [C# MVP]

Rafael Veronezi said:
Just to fix, correct me if I am wrong...
With reference types (objects), if I assign an object to another, the
assignment will be the address of the object, and not a copy of it's
contents right?

Yup - but to make the thinking easier, you never actually assign an
object to another. You assign the value of one variable to another
variable. The values of those variables are references to start with
though.
With value types (structs), if I assign an object to another, I'll be
copying it's data to the left side of the assignment, and not it's address.

Again, you're assigning the value of one variable to another - but in
this case, the value of the variable is the actual data.
This is valid with method parameters too, like, if I pass an object as a
parameter, i'll be passing a reference to the memory point where the object
is allocated, and every change made to the object in the method, will be
reflected when the method returns, again with Value Types, if I pass a value
type as a method parameter, I'm passing only the reference, in this case I
need to specify out or ref keyword in the parameter, to indicate that I want
to pass it by reference...

Few questions...
With value types, if I pass an out parameter for example, I'm passing it's
memory address to, or it works with some kind of boxing/unboxing technique
to pass these parameters? It's was just a thought, don't know if it make
sense, because I don't think that it will need to box the value type to pass
that as a reference... anyways...

See http://www.pobox.com/~skeet/csharp/parameters.html for a careful
and hopefully pretty thorough treatment of parameter passing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top