References vs Copies

  • Thread starter Thread starter mehdi_mousavi
  • Start date Start date
Kevin said:
I understand what you're saying, Jon. I didn't want to get into a long
discussion of pointers, which many .Net developers are not very familiar
with. Yes, a reference type is passed by value, but it is the value of the
pointer (the address of the class instance), not the class instance, that is
passed, which effectively means that a reference to the class instance is
passed, since the pointer is simply a variable that represents (points to)
the class instance.

While a value type is (technically) referenced by a pointer as well

Not sure what you mean here - the variable itself (assuming one is
involved) is effectively a pointer, but the *value* of that variable is
the actual value, not a pointer.
the value of the instance is copied when it is passed, and the new instance
variable does not point to the same instance as the original.

<snip>

Indeed. I believe we're "on the same page" as it were, but calling the
reference type behaviour "pass by reference" has (justifiably) confused
people in the past. I've always found that explaining that it really
*is* pass-by-value semantics, but that it's a reference which is
passed, helps both those who are new *and* those who understand true
"pass by reference" semantics. It also makes it easier to explain what
happens when you pass a reference type parameter by reference with the
"ref" keyword :)

Jon
 
Not sure what you mean here - the variable itself (assuming one is
involved) is effectively a pointer, but the *value* of that variable is
the actual value, not a pointer.

Exactly (what I meant), and that is where the confusion lies when discussing
or learning the topic.

As to how to explain it, I'm sure your technique is as good as any other. I
just don't know the best way to explain it. Pointers are to programming as
Vectors are to mathematics. You spend a good bit of your lifetime learning
to think about things in one way, and then when you get down to the
nitty-gritty, you find that the way you thought about them was a
convenience. Then you have to adjust the way you think. After that, it's
fine again.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
Kevin said:
Exactly (what I meant), and that is where the confusion lies when discussing
or learning the topic.

As to how to explain it, I'm sure your technique is as good as any other. I
just don't know the best way to explain it. Pointers are to programming as
Vectors are to mathematics. You spend a good bit of your lifetime learning
to think about things in one way, and then when you get down to the
nitty-gritty, you find that the way you thought about them was a
convenience. Then you have to adjust the way you think. After that, it's
fine again.

Well, at least you didn't say, "Pointers are like thermodynamics...."
:-)
 
Back
Top