Take the 2-minute tour ×

When I do the following.. anything done to Person b modifies Person a (I thought doing this would clone Person b from Person a). I also have NO idea if changing Person a will change Person b after the linking. Due to my code right now, I can only see this in 1 direction.

Person a = new Person() { head = "big", feet = "small" };
Person b = a; 

b.head = "small"; //now a.head = "small" too   

Now if I do this instead.. Person a becomes completely separate.

Person b = new Person() { head = a.head, feet = a.feet };

Now this fine and kinda makes sense when comparing this behaviour to other things in C#. BUT, this could get very annoying with large objects.

Is there a way to shortcut this at all?

Such as:

Person b = a.Values;

share|improve this question
Can you give an example of what you're trying to accomplish with this? This isn't the sort of thing that's needed frequently. Maybe there's another way to accomplish your task. –  John Saunders Mar 19 '11 at 1:17
It is called "deep copy", search for this. Your example is a very good one for why you rarely actually do this. The odds that one person would have the exact same traits as another one are quite rare. –  Hans Passant Mar 19 '11 at 1:18
Note that the word "linking" is not used for what you're doing there. This is an assignment. The question header implies that the question has something to do with the linker. –  steinar Mar 19 '11 at 1:22
Sorry about the terminology, I don't know how to properly define everything yet. I'm basically storing a bunch of settings in a object. Then another object takes those settings and builds itself from them. Unfortunately (for me right now), building the second object changes the first object because of this behavior. Maybe I shouldn't be storing my settings in an object, but I don't know how else to do it because of how complex it is. –  PiZzL3 Mar 19 '11 at 1:30
This isn't a link at all. They're the same object. You've got two references to the same object. –  John Saunders Mar 19 '11 at 1:30
add comment

5 Answers

up vote 9 down vote accepted

Is there a way to shortcut this at all?

No, not really. You'll need to make a new instance in order to avoid the original from affecting the "copy". There are a couple of options for this:

  1. If your type is a struct, not a class, it will be copied by value (instead of just copying the reference to the instance). This will give it the semantics you're describing, but has many other side effects that tend to be less than desirable, and is not recommended for any mutable type (which this obviously is, or this wouldn't be an issue!)

  2. Implement a "cloning" mechanism on your types. This can be ICloneable or even just a constructor that takes an instance and copies values from it.

  3. Use reflection, MemberwiseClone, or similar to copy all values across, so you don't have to write the code to do this. This has potential problems, especially if you have fields containing non-simple types.

share|improve this answer
Thanks, that sums it up nicely. I think I'll have to figure out a different way to store my data or make a special constructor. –  PiZzL3 Mar 19 '11 at 2:11
add comment

What you are looking is for a Cloning. You will need to Implement IClonable and then do the Cloning.


class Person() : ICloneable
    public string head;
    public string feet; 

    #region ICloneable Members

    public object Clone()
        return this.MemberwiseClone();


Then You can simply call the Clone method to do a ShallowCopy (In this particular Case also a DeepCopy)

Person b = (Person) a.Clone();  

You can use the MemberwiseClone method of the Object class to do the cloning.

share|improve this answer
Do not use IClonable: blogs.msdn.com/b/brada/archive/2003/04/09/49935.aspx –  Robert Levy Mar 19 '11 at 1:26
@Robert Levy: I know the difference arise between Shallow Copy and Deep Copy, when you have Reference type of members in the class. –  Shekhar_Pro Mar 19 '11 at 1:32
add comment

a and b are just two references to the same Person object. They both essentially hold the address of the Person.

There is a ICloneable interface, though relatively few classes support it. With this, you would write:

Person b = a.Clone();

Then, b would be an entirely separate Person.

You could also implement a copy constructor:

public Person(Person src)
  // ... 

There is no built-in way to copy all the fields. You can do it through reflection, but there would be a performance penalty.

share|improve this answer
@Robert, 1. The difference between deep copy and shallow copy is irrelevant here, because the class only has two fields, both with immutable types. 2. I didn't recommend ICloneable. I listed it as an option. 3. That is not an official guideline, just a discussion of a plan to add it to the official guidelines (I don't know if it made it). –  Matthew Flaschen Mar 19 '11 at 1:37
it did make it to the official guidelines –  Robert Levy Mar 19 '11 at 2:07
@Robert, can you post the official link, so I can see the details? –  Matthew Flaschen Mar 19 '11 at 2:47
section 8.5 of the book (second edition) amazon.com/Framework-Design-Guidelines-Conventions-Libraries/dp/… –  Robert Levy Mar 19 '11 at 3:32
show 1 more comment

You could do it like this:

var jss = new JavaScriptSerializer();
var b = jss.Deserialize<Person>(jss.Serialize(a));

For deep cloning you may want to take a look at this answer: http://stackoverflow.com/a/78612/550975

share|improve this answer
add comment

This happens because "Person" is a class, so it is passed by reference. In the statement "b = a" you are just copying a reference to the one and only "Person" instance that you created with the keyword new.

The easiest way to have the behavior that you are looking for is to use a "value type".

Just change the Person declaration from

class Person


struct Person
share|improve this answer
This just makes it harder to have a class. 'Cannot have instance initalizers in structs' –  Bonzo Oct 6 '12 at 18:34
add comment

Your Answer


