One of the hardest concepts for people to understand when it
comes to reference types is assignment. It is often difficult to realize how
assignments work with reference types. The problem with this is that a lot of
people assume that value types and reference types handle assignment in the
same way. This is wrong. Assignments with value types copy the value stored in
the memory location. Reference types merely copy the reference to the value.
Basic assignment of reference types
When you assign a variable which is a reference type to
another variable of the same type, it does not duplicate the values. What
happens behind the scenes is that the pointer to the memory location is
duplicated.
In the following code I create two variables; both of them
are strings. The first one I start by giving a string value. The second one I start
by assigning null. I then assign it to the first variable. In the accompanying
illustration you can see what changes after the assignment.
Listing 1: Reference Assignment
string myName = "Brendan";
string authorName = null;
authorName = myName;
Figure 2: Reference Assignment

It is important to notice here that we have not copied the
data. All we have done is copied that reference. This means that we are
pointing at the exact same data for both variables. I have seen many people run
into problems where they assumed that an assignment with reference type
variables worked the same way as value types. They assume the value has been
duplicated. A dangerous assumption since they may alter the value not realizing
it alters both.