Was looking through a C# book today and come across something like this:
class Circle...
Circle c = x
Circle d = c
My understanding is:
o Both c,d contain the same value x
The variables c and d both contain the same value that the variable x
contains. That is, either null or a reference to a specific instance of
Circle. Let's assume it's non-null.
o But this is the exact same value of x
The value is the reference to the instance. Don't confuse the reference
with the instance itself. None of those variables contain the instance.
They all contain a reference to the instance.
o Assignment d=c would do the same
Yes.
o If I change part of c, then d also changes?
You can't change just part of c. The variable c is a reference, and can
only be assigned to null or another reference.
Now, probably what you meant was "if I change part of the instance that c
references, then part of the instance that d references also changes".
And of course this is true because c and d both reference the same
instance.
It's sort of like a conference call (and only "sort of", mind you

).
Suppose you've got three people on the phone together, call them A, B, and
C. A and B both, through their phone connection to C, "reference" C.
Whatever C says, both A and B hear, because they are referencing the same
entity.
Likewise, your variables c and d both reference the same entity (the
instance of Circle originally referenced by x), and so whatever that
instance of Circle "says", you'll be able to see via either c or d. Of
course, you can use either c or d to affect what that instance of Circle
"says" as well.
Yes. Any variable referencing that instance will be able to see any
change to that instance.
o Completely changing c, as in c=y, would still leave d == x?
Yes.
o This is different behaviour from ordinary non-class variables
Well, in C# I'd say that class variables are the "ordinary" ones.

But
yes, as compared to value types, this behavior is different.
If I am correct, why would this ever be useful?
Well, one big benefit is that you don't have to copy a bunch of data
around, which would be inefficient. The other benefit is that it's quite
common to have different pieces of code that all want to manipulate the
same data. It would be a coordination nightmare to have to keep all the
different copies in synchronization. Using some kind of referencing
mechanism allows there to be just one copy of the data, accessible by any
code that has a reference to it.
I assume there must be a way of duplicating the value when assigning?
If the class supports it, yes. Not all classes do. Usually, they will
implement the ICloneable interface if they do, which means they have a
Clone() method in the class. Calling the Clone() method creates a brand
new instance identical to the original. Then changes can be made to that
new instance without affecting the original.
Jon Skeet has an article that is actually focused mainly on parameter
passing. But because it addresses in some detail the difference between
value types and reference types, it's applicable to a question like
yours. You can see it here:
http://www.pobox.com/~skeet/csharp/parameters.html
Pete