Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Clone Objects

Tags:

c#

object

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;

like image 808
PiZzL3 Avatar asked Mar 19 '11 01:03

PiZzL3


People also ask

How we can clone a object in Java?

Creating a copy using the clone() method The class whose object's copy is to be made must have a public clone method in it or in one of its parent class. Every class that implements clone() should call super. clone() to obtain the cloned object reference. The class must also implement java.

How do you clone items in Roblox?

First, get a reference to the original object. Then, make a copy of the object and insert the copy by setting its Parent to the Workspace or one of its descendants. Finally, when it's time to regenerate the model, Destroy the copy and clone a new one from the original like before.

How do you clone an object in Java 8?

You can also define copy constructor if your class has mostly mutable properties. Utilize Object clone() method by calling super. clone() in overridden clone method, then make necessary changes for deep copying of mutable fields. If your class is serializable, you can use serialization for cloning.


2 Answers

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

Example:

class Person() : ICloneable {     public string head;     public string feet;       #region ICloneable Members      public object Clone()     {         return this.MemberwiseClone();     }      #endregion } 

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

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

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

like image 78
Shekhar_Pro Avatar answered Oct 16 '22 22:10

Shekhar_Pro


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.

like image 37
Reed Copsey Avatar answered Oct 17 '22 00:10

Reed Copsey